├── .DS_Store ├── Doubt-Session-2 ├── ArmstrongNumber ├── ArmstrongNumber.cp ├── BostonNumber.cpp ├── LCM.cpp ├── PascalTriangle ├── PascalTriangle.cpp ├── PatternStar └── PatternStar.cpp ├── DoubtSession-1 ├── GCD ├── GCD.cpp ├── LCM ├── LCM.cpp ├── PascalTriangle ├── PascalTriangle.cpp ├── SquareRoot └── SquareRoot.cpp ├── Lecture-1 ├── HelloWorld ├── HelloWorld.cpp ├── input └── input.cpp ├── Lecture-10 ├── AddressOf ├── AddressOf.cpp ├── ArrayAndPointers ├── ArrayAndPointers.cpp ├── Functions ├── Functions.cpp ├── Pointers ├── Pointers.cpp ├── Test └── Test.cpp ├── Lecture-11 ├── CharacterArrayInput ├── CharacterArrayInput.cpp ├── DefaultArguments ├── DefaultArguments.cpp ├── Largest ├── Largest.cpp ├── NumberAndString ├── NumberAndString.cpp ├── Pallindrome ├── Pallindrome.cpp ├── StringAppend ├── StringAppend.cpp ├── StringLength ├── StringLength.cpp ├── Test └── Test.cpp ├── Lecture-12 ├── 2DArray ├── 2DArray.cpp ├── 2DArrayInput ├── 2DArrayInput.cpp ├── PermutationOrNot ├── PermutationOrNot.cpp ├── ReverseString ├── ReverseString.cpp ├── RotateString ├── RotateString.cpp ├── Substrings ├── Substrings.cpp ├── Test └── Test.cpp ├── Lecture-13 ├── FunctionsCharacter ├── FunctionsCharacter.cpp ├── SortStrings ├── SortStrings.cpp ├── SpiralPrint ├── SpiralPrint.cpp ├── String ├── String-1 ├── String-1.cpp ├── String.cpp ├── StringArray ├── StringArray.cpp ├── Test ├── Test.cpp ├── WavePrint └── WavePrint.cpp ├── Lecture-14 ├── Factorial ├── Factorial.cpp ├── Fibonacci ├── Fibonacci.cpp ├── PrintDecreasing ├── PrintDecreasing.cpp ├── SumOfNNumbers ├── SumOfNNumbers.cpp └── Test.cpp ├── Lecture-15 ├── ArraySorted ├── ArraySorted.cpp ├── Check7 ├── Check7.cpp ├── First7 ├── First7.cpp ├── IntToString ├── IntToString.cpp ├── SumOfArray ├── SumOfArray.cpp └── Test.cpp ├── Lecture-16 ├── BinarySearch ├── BinarySearch.cpp ├── BubbleSort ├── BubbleSort.cpp ├── GUNGUN ├── GUNGUN.cpp ├── Last7 ├── Last7.cpp ├── MergeSortedArrays ├── MergeSortedArrays.cpp ├── Multiply ├── Multiply.cpp ├── StringToInt └── StringToInt.cpp ├── Lecture-17 ├── MergeSort ├── MergeSort.cpp ├── Permutation ├── Permutation.cpp ├── Subsequences ├── Subsequences.cpp ├── Test.cpp ├── toh └── toh.cpp ├── Lecture-19 ├── RatInMaze ├── RatInMaze.cpp ├── RatInMazeAdvanced └── RatInMazeAdvanced.cpp ├── Lecture-2 ├── CheckPrime ├── CheckPrime.cpp ├── Conditionals ├── Conditionals.cpp ├── LargestOfN ├── LargestOfN.cpp ├── Pattern123 ├── Pattern123.cpp ├── PatternStar ├── PatternStar.cpp ├── PrintAllEven ├── PrintAllEven.cpp ├── SimpleInterest ├── SimpleInterest.cpp ├── Test.cpp ├── WhileLoop ├── WhileLoop.cpp └── untitled ├── Lecture-20 ├── MazeWays ├── MazeWays.cpp ├── NStairCase ├── NStairCase.cpp ├── SudokuSolver ├── SudokuSolver.cpp ├── Test └── Test.cpp ├── Lecture-21 ├── 2DVector ├── 2DVector-Array ├── 2DVector-Array.cpp ├── 2DVector.cpp ├── ElephantWays ├── ElephantWays.cpp ├── Jayani ├── Jayani.cpp ├── PhoneKeypad ├── PhoneKeypad.cpp ├── Vector-Array ├── Vector-Array.cpp ├── Vectors └── Vectors.cpp ├── Lecture-22 ├── CombinationSum.cpp ├── CombinationSum2. ├── CombinationSum2.cpp ├── Combinations ├── Combinations.cpp └── Test.cpp ├── Lecture-23 ├── DMA2D ├── DMA2D.cpp ├── OOPS ├── OOPS.cpp ├── Test └── Test.cpp ├── Lecture-24 ├── OOPS └── OOPS.cpp ├── Lecture-25 ├── OOPs ├── OOPs.cpp ├── Vectors └── Vectors.cpp ├── Lecture-26 ├── Test └── Test.cpp ├── Lecture-27 ├── LinkedList ├── LinkedList.cpp └── Test.cpp ├── Lecture-28 ├── LinkedList.cpp ├── Subsets-2.cpp └── Subsets.cpp ├── Lecture-29 ├── LinkedList └── LinkedList.cpp ├── Lecture-3 ├── AsciiCodes ├── AsciiCodes.cpp ├── CelAndFah ├── CelAndFah.cpp ├── SquareRoot ├── SquareRoot.cpp ├── StarPattern ├── StarPattern.cpp ├── Test.cpp ├── UpperAndLower ├── UpperAndLower.cpp ├── UpperAndLowercase-1 └── UpperAndLowercase-1.cpp ├── Lecture-30 ├── BalancedParanthesis ├── BalancedParanthesis.cpp ├── Queue ├── Queue-LL ├── Queue-LL.cpp ├── Queue.cpp ├── ReverseStack ├── ReverseStack.cpp ├── Stack-LL ├── Stack-LL.cpp ├── Stack-Vector ├── Stack-Vector.cpp ├── Test └── Test.cpp ├── Lecture-31(QPS) ├── FirstMissing.cpp ├── PalindromePartition.cpp ├── Permutation.cpp └── wordsearch.cpp ├── Lecture-32 ├── BinaryTrees ├── BinaryTrees.cpp ├── CircularQueue ├── CircularQueue.cpp ├── StackUsingQueue └── StackUsingQueue.cpp ├── Lecture-33 ├── BinaryTrees ├── BinaryTrees.cpp └── input.txt ├── Lecture-34(QPS) ├── Subsets.cpp └── Test.cpp ├── Lecture-35(BinaryTrees) ├── BST ├── BST.cpp ├── Test ├── Test.cpp └── input.txt ├── Lecture-36(NST) ├── BST ├── BST.cpp ├── PriorityQueue.cpp └── input.txt ├── Lecture-37(QPS) ├── Histogram.cpp └── Test.cpp ├── Lecture-38(Heaps) ├── Heap ├── Heap.cpp ├── NewHeap ├── NewHeap.cpp ├── PriorityQueue ├── PriorityQueue.cpp ├── TopKElements └── TopKElements.cpp ├── Lecture-39(Hashmaps) ├── Hashmap ├── Hashmap.cpp ├── Ordered_map ├── Ordered_map.cpp ├── Test ├── Test.cpp ├── hashmapInsertion └── hashmapInsertion.cpp ├── Lecture-4 ├── BitwiseOperators ├── BitwiseOperators.cpp ├── CompoundAssignment.cpp ├── Constants ├── Constants.cpp ├── CountSetBits ├── CountSetBits.cpp ├── ExplicitTypeConversion ├── ExplicitTypeConversion.cpp ├── Macros ├── Macros.cpp ├── SizeOf ├── SizeOf.cpp └── Test.cpp ├── Lecture-40(QPS) ├── ConsecutiveOnes.cpp ├── KDistinctChar.cpp ├── LongestNonRepeatingChar.cpp ├── MaxSlidingWindow.cpp └── Test.cpp ├── Lecture-41(DP) ├── CoinExchange ├── CoinExchange.cpp ├── MinStepsTo1 ├── MinStepsTo1.cpp ├── Test ├── Test-1 ├── Test-1.cpp └── Test.cpp ├── Lecture-42(DP) ├── CoinExchange ├── CoinExchange.cpp ├── WineProblem └── WineProblem.cpp ├── Lecture-43(DP) ├── Knapsack ├── Knapsack.cpp ├── LCS └── LCS.cpp ├── Lecture-44(Graphs) ├── GenericGraph ├── GenericGraph.cpp ├── Graphs ├── Graphs.cpp ├── list ├── list.cpp └── untitled ├── Lecture-45(Graphs) ├── Dijkstras ├── Dijkstras.cpp ├── GenericGraph ├── GenericGraph.cpp ├── WeightedGraph ├── WeightedGraph.cpp ├── set ├── set.cpp ├── set_Pair └── set_Pair.cpp ├── Lecture-5 ├── CheckPrime ├── CheckPrime.cpp ├── ForLoop ├── ForLoop.cpp ├── PrintAllPrimes ├── PrintAllPrimes.cpp ├── PrintPattern ├── PrintPattern-1 ├── PrintPattern-1.cpp ├── PrintPattern.cpp ├── ReverseNumber ├── ReverseNumber.cpp ├── Test.cpp ├── UniqueNumber-1 ├── UniqueNumber-1.cpp ├── Yash └── Yash.cpp ├── Lecture-6 ├── AsciiCode ├── AsciiCode.cpp ├── CountAllCharacters ├── CountAllCharacters.cpp ├── CountCharacters ├── CountCharacters.cpp ├── PatternStar ├── PatternStar.cpp ├── Scopes ├── Scopes.cpp ├── Test └── Test.cpp ├── Lecture-7 ├── ArrayInitialization ├── ArrayInitialization.cpp ├── Arrays ├── Arrays.cpp ├── BubbleSort ├── BubbleSort.cpp ├── Directions ├── Directions.cpp ├── FindLargest ├── FindLargest.cpp ├── InsertionSort ├── InsertionSort.cpp ├── LinearSearch ├── LinearSearch-1 ├── LinearSearch-1.cpp ├── LinearSearch.cpp ├── ReverseNumbers ├── ReverseNumbers.cpp ├── Test.cpp ├── UserInput ├── UserInput.cpp └── doWhile.cpp ├── Lecture-8 ├── CelAndFah ├── CelAndFah.cpp ├── Functions ├── Functions.cpp ├── SelectionSort ├── SelectionSort.cpp ├── SwitchCase ├── SwitchCase.cpp ├── TernaryOperator ├── TernaryOperator.cpp ├── doWhile └── doWhile.cpp ├── Lecture-9 ├── ArrayFunctions ├── ArrayFunctions.cpp ├── ArraySum ├── ArraySum.cpp ├── BinarySearch ├── BinarySearch.cpp ├── CallByValRef ├── CallByValRef.cpp ├── CelAndFah ├── CelAndFah.cpp ├── CheckPrime ├── CheckPrime.cpp ├── Factorial ├── Factorial.cpp ├── ForwardDeclaration ├── ForwardDeclaration.cpp ├── Functions ├── Functions.cpp ├── MergeSorted ├── MergeSorted.cpp ├── PrintAllPrimes ├── PrintAllPrimes.cpp ├── SelectionSort ├── SelectionSort.cpp └── Test.cpp ├── QPs-DP ├── Coins.cpp ├── PallindromePartition-2.cpp └── Test.cpp └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/.DS_Store -------------------------------------------------------------------------------- /Doubt-Session-2/ArmstrongNumber: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Doubt-Session-2/ArmstrongNumber -------------------------------------------------------------------------------- /Doubt-Session-2/ArmstrongNumber.cp: -------------------------------------------------------------------------------- 1 | // ArmstrongNumber.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | int n, no; 9 | cin >> n; 10 | 11 | no = n; 12 | 13 | int sum = 0, digits_cnt = 0; 14 | 15 | while (n > 0) { 16 | 17 | digits_cnt++; 18 | n /= 10; 19 | } 20 | 21 | n = no; 22 | int y = digits_cnt; 23 | 24 | while (n > 0) { 25 | int ld = n % 10; 26 | int x = pow(ld, y); 27 | sum += x; 28 | 29 | n /= 10; 30 | } 31 | 32 | // cout << sum << endl; 33 | if (sum == no) { 34 | cout << "true" << endl; 35 | } 36 | else { 37 | cout << "false" << endl; 38 | } 39 | 40 | 41 | 42 | return 0; 43 | } 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Doubt-Session-2/BostonNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | int no = n; 11 | 12 | // Finding the sum of digits 13 | int sum_d = 0; 14 | while (n > 0) { 15 | 16 | int digit = n % 10; 17 | sum_d += digit; 18 | n /= 10; 19 | } 20 | 21 | 22 | // Finding the sum of prime factors 23 | n = no; 24 | int sum = 0; 25 | while (n % 2 == 0) { 26 | sum += 2; 27 | n /= 2; 28 | } 29 | for (int i = 3; i <= sqrt(n); i += 2) 30 | { 31 | while (n % i == 0) { 32 | // Add the value of i to the sum, also break the i into single 33 | // digits if i contains digits more than 2 in itself 34 | int x = i; 35 | while (x > 0) { 36 | sum += x % 10; 37 | x /= 10; 38 | } 39 | 40 | n /= i; 41 | } 42 | } 43 | 44 | if (n > 0) { 45 | while (n > 0) { 46 | sum += n % 10; 47 | n /= 10; 48 | } 49 | } 50 | 51 | if (sum == sum_d) { 52 | cout << "1" << endl; 53 | } 54 | else { 55 | cout << "0" << endl; 56 | } 57 | 58 | return 0; 59 | } 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /Doubt-Session-2/LCM.cpp: -------------------------------------------------------------------------------- 1 | // LCM.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // LCM ka way 1 7 | // int no1 = 2, no2 = 3; 8 | cin >> no1 >> no2; 9 | 10 | int x = no2; 11 | while ( (x % no1) != 0) { 12 | x = x + no2; 13 | } 14 | 15 | cout << "LCM: " << x << endl; 16 | int y = no1 * no2 / x; 17 | cout << "GCD: " << y << endl; 18 | 19 | 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Doubt-Session-2/PascalTriangle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Doubt-Session-2/PascalTriangle -------------------------------------------------------------------------------- /Doubt-Session-2/PascalTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n, row, j; 7 | 8 | int rows, nt, pt; 9 | cin >> n; 10 | 11 | for (int rows = 0; rows < n; ++rows) 12 | { 13 | int pt = 1; 14 | cout << 1 << " "; 15 | for (j = 1 ; j <= rows ; j++) { 16 | nt = pt * (rows - j + 1) / j; 17 | cout << nt << " "; 18 | 19 | pt = nt; 20 | } 21 | cout << endl; 22 | } 23 | 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Doubt-Session-2/PatternStar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Doubt-Session-2/PatternStar -------------------------------------------------------------------------------- /Doubt-Session-2/PatternStar.cpp: -------------------------------------------------------------------------------- 1 | // PatternStar.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | for (int row = 1; row <= (n + 1) / 2; ++row) 11 | { 12 | if (row == 1) { 13 | for (int i = 1; i <= n; ++i) 14 | { 15 | cout << "*\t"; 16 | } 17 | } 18 | else { 19 | // 1. Print (n+1)/2 - row + 1'*' 20 | for (int i = 1; i <= (n + 1) / 2 - row + 1; ++i) 21 | { 22 | cout << "*\t"; 23 | } 24 | // 2. Print 2*(row-1)-1, times spaces 25 | for (int i = 1; i <= 2 * (row - 1) - 1; ++i) 26 | { 27 | cout << "\t"; 28 | } 29 | // 3. Print (n+1)/2 - row + 1 '*' 30 | for (int i = 1; i <= (n + 1) / 2 - row + 1; ++i) 31 | { 32 | cout << "*\t"; 33 | } 34 | } 35 | cout << endl; 36 | } 37 | 38 | 39 | for (int row = (n + 1) / 2 - 1; row >= 1; --row) 40 | { 41 | if (row == 1) { 42 | for (int i = 1; i <= n; ++i) 43 | { 44 | cout << "*\t"; 45 | } 46 | } 47 | else { 48 | // 1. Print (n+1)/2 - row + 1'*' 49 | for (int i = 1; i <= (n + 1) / 2 - row + 1; ++i) 50 | { 51 | cout << "*\t"; 52 | } 53 | // 2. Print 2*(row-1)-1, times spaces 54 | for (int i = 1; i <= 2 * (row - 1) - 1; ++i) 55 | { 56 | cout << "\t"; 57 | } 58 | // 3. Print (n+1)/2 - row + 1 '*' 59 | for (int i = 1; i <= (n + 1) / 2 - row + 1; ++i) 60 | { 61 | cout << "*\t"; 62 | } 63 | } 64 | cout << endl; 65 | } 66 | 67 | return 0; 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /DoubtSession-1/GCD: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/DoubtSession-1/GCD -------------------------------------------------------------------------------- /DoubtSession-1/GCD.cpp: -------------------------------------------------------------------------------- 1 | // GCD.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int no1 = 10, no2 = 15; 8 | int ans; 9 | 10 | int i; 11 | int chotaNumber; 12 | if (no1 > no2) { 13 | chotaNumber = no2; 14 | } 15 | else { 16 | chotaNumber = no1; 17 | } 18 | 19 | bool kyaGCDmilla = false; 20 | i = 2; 21 | while (i <= chotaNumber) { 22 | if ( (no1 % i == 0) and (no2 % i == 0) ) { 23 | ans = i; 24 | kyaGCDmilla = true; 25 | } 26 | 27 | i = i + 1; 28 | } 29 | if (kyaGCDmilla == true) { 30 | cout << "GCD: " << ans << endl; 31 | cout << "LCM: " << no1*no2 / ans << endl; 32 | } 33 | else { 34 | cout << "GCD: " << 1 << endl; 35 | cout << "LCM: " << no1*no2 << endl; 36 | } 37 | 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /DoubtSession-1/LCM: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/DoubtSession-1/LCM -------------------------------------------------------------------------------- /DoubtSession-1/LCM.cpp: -------------------------------------------------------------------------------- 1 | // LCM.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // LCM ka way 1 7 | // int no1 = 2, no2 = 3; 8 | cin >> no1 >> no2; 9 | 10 | int x = no2; 11 | while ( (x % no1) != 0) { 12 | x = x + no2; 13 | } 14 | 15 | cout << "LCM: " << x << endl; 16 | int y = no1 * no2 / x; 17 | cout << "GCD: " << y << endl; 18 | 19 | 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /DoubtSession-1/PascalTriangle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/DoubtSession-1/PascalTriangle -------------------------------------------------------------------------------- /DoubtSession-1/PascalTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n, r; 7 | 8 | int rows, nt, pt; 9 | cin >> rows; 10 | 11 | n = 0; 12 | while (n < rows) { 13 | 14 | int pt = 1; 15 | cout << 1 << " "; 16 | r = 1; 17 | while (r <= n) { 18 | nt = pt * (n - r + 1) / r; 19 | cout << nt << " "; 20 | 21 | pt = nt; 22 | r++; 23 | } 24 | cout << endl; 25 | n++; 26 | } 27 | 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /DoubtSession-1/SquareRoot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/DoubtSession-1/SquareRoot -------------------------------------------------------------------------------- /DoubtSession-1/SquareRoot.cpp: -------------------------------------------------------------------------------- 1 | // SquareRoot.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | float i, inc = 1; 8 | 9 | int j; 10 | int n; 11 | cin >> n; 12 | 13 | j = 1; 14 | while (j <= 3) { 15 | 16 | while (i * i <= n) { 17 | i = i + inc; 18 | } 19 | i = i - inc; 20 | 21 | inc = inc / 10; 22 | j++; 23 | } 24 | cout << i << endl; 25 | return 0; 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Lecture-1/HelloWorld: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-1/HelloWorld -------------------------------------------------------------------------------- /Lecture-1/HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int x; 7 | cout << x << endl; 8 | 9 | // Intialization:Bucket banate time usmei value dena 10 | int a = 10; 11 | 12 | // Assignment: Bucket bani hui hai usse value dedo 13 | int y; 14 | y = 20; 15 | 16 | // cout << "Hello World"; 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lecture-1/input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-1/input -------------------------------------------------------------------------------- /Lecture-1/input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // int n; 7 | // int r; 8 | int n, r; 9 | // cin >> n; 10 | // cin >> r; 11 | cin >> n >> r; 12 | 13 | // cout << "User ne enter kia: "; 14 | // cout << n; 15 | cout << "User ne enter kia: " << n << '\n'; 16 | cout << "Value of r: " << r << '\n'; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Lecture-10/AddressOf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-10/AddressOf -------------------------------------------------------------------------------- /Lecture-10/AddressOf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int arr[] = {1, 2, 3, 4}; 6 | int a = 10; 7 | float f = 1.11; 8 | 9 | char ch = 'A'; 10 | 11 | cout << "Address of a: " << &a << endl; 12 | cout << "Address of f: " << &f << endl; 13 | cout << "Address of ch: " << &ch << endl; 14 | cout << "Address of arr: " << arr << endl; 15 | 16 | 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lecture-10/ArrayAndPointers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-10/ArrayAndPointers -------------------------------------------------------------------------------- /Lecture-10/ArrayAndPointers.cpp: -------------------------------------------------------------------------------- 1 | // ArrayAndPointers.cpp 2 | #include 3 | using namespace std; 4 | 5 | void printArray(int *a, int n) { 6 | for (int i = 0; i < n; ++i) 7 | { 8 | // cout << *(a + i) << " "; 9 | cout << a[i] << " "; 10 | } 11 | cout << endl; 12 | } 13 | 14 | int main() { 15 | 16 | int a[] = {1, 2, 3, 4, 5}; 17 | int n = sizeof(a) / sizeof(int); 18 | 19 | printArray(a, n); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-10/Functions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-10/Functions -------------------------------------------------------------------------------- /Lecture-10/Functions.cpp: -------------------------------------------------------------------------------- 1 | // Functions.cpp 2 | #include 3 | using namespace std; 4 | 5 | void update(int *ax) { 6 | *ax = *ax + 5; 7 | } 8 | 9 | int main() { 10 | 11 | int a = 10; 12 | int* aptr = &a; 13 | 14 | cout << *aptr << endl; 15 | 16 | update(aptr); 17 | 18 | cout << *aptr << endl; 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Lecture-10/Pointers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-10/Pointers -------------------------------------------------------------------------------- /Lecture-10/Pointers.cpp: -------------------------------------------------------------------------------- 1 | // Pointers.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // int *a, *x, *y, *z, l; // a,x,y,z are pointers and l is an integer 7 | 8 | int a = 10; 9 | float f = 1.11; 10 | char ch = 'A'; 11 | 12 | int *ap = &a; 13 | float *fp = &f; 14 | char *chp = &ch; 15 | 16 | cout << "&a: " << &a << endl; 17 | cout << "ap: " << ap << endl; 18 | cout << "&f: " << &f << endl; 19 | cout << "fp: " << fp << endl; 20 | cout << "&ch: " << (int*)&ch << endl; 21 | cout << "chp: " << (int*)chp << endl; 22 | 23 | // Pointers and garbage value 24 | int *pt = NULL; 25 | cout << *pt << endl; 26 | // if (*pt > 1) { 27 | // cout << "Take left turn\n"; 28 | // } 29 | // else { 30 | // cout << "Take Right turn\n"; 31 | // } 32 | // cout << *ap + 10 << endl; 33 | 34 | 35 | 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Lecture-10/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-10/Test -------------------------------------------------------------------------------- /Lecture-10/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printPair(int a[], int n, int X) { 5 | for (int i = 0; i < n - 1; ++i) 6 | { 7 | int no1 = a[i]; 8 | for (int j = i + 1; j < n; ++j) 9 | { 10 | int no2 = a[j]; 11 | if (no1 + no2 == X) { 12 | cout << "(" << no1 << ", " << no2 << ") "; 13 | } 14 | } 15 | } 16 | } 17 | 18 | int main() { 19 | 20 | int a[] = {1, 3, 2, 2, 0, 4}; // (1,3) (2,2) (0,4) 21 | int n = sizeof(a) / sizeof(int); 22 | 23 | int X = 4; 24 | 25 | printPair(a, n, X); 26 | 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Lecture-11/CharacterArrayInput: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/CharacterArrayInput -------------------------------------------------------------------------------- /Lecture-11/CharacterArrayInput.cpp: -------------------------------------------------------------------------------- 1 | // CharacterArrayInput.cpp 2 | #include 3 | using namespace std; 4 | 5 | void ReadString(char *a, int s, char delimiter = '\n') { 6 | int i = 0; 7 | char ch = cin.get(); 8 | while (i < s - 1 and ch != delimiter) { 9 | a[i] = ch; 10 | i++; 11 | 12 | ch = cin.get(); 13 | } 14 | a[i] = '\0'; 15 | } 16 | 17 | int main() { 18 | 19 | char a[100]; 20 | 21 | // ReadString(a, 100 , '@'); 22 | cin.getline(a, 100, '$'); 23 | cout << a << endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Lecture-11/DefaultArguments: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/DefaultArguments -------------------------------------------------------------------------------- /Lecture-11/DefaultArguments.cpp: -------------------------------------------------------------------------------- 1 | // DefaultArguments.cpp 2 | #include 3 | using namespace std; 4 | 5 | int add(int a = 0, int b = 0, int c = 0) { 6 | return a + b + c; 7 | } 8 | 9 | int main() { 10 | 11 | cout << add(1, 2, 3) << endl; 12 | cout << add(1, 2) << endl; 13 | cout << add(1) << endl; 14 | cout << add() << endl; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lecture-11/Largest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/Largest -------------------------------------------------------------------------------- /Lecture-11/Largest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Length(char *a) { 5 | int cnt = 0; 6 | 7 | for (int i = 0; a[i] != '\0' ; ++i) 8 | { 9 | cnt++; 10 | } 11 | return cnt; 12 | } 13 | 14 | void Copy(char *largest, char *a) { 15 | int lena = Length(a); 16 | for (int i = 0; i <= lena; ++i) 17 | { 18 | largest[i] = a[i]; 19 | } 20 | } 21 | 22 | int main() { 23 | char a[100]; 24 | int large_len = 0; 25 | char largest[1000]; 26 | 27 | int n; 28 | cin >> n; 29 | cin.ignore(); // or cin.get() 30 | 31 | for (int i = 0; i < n; ++i) 32 | { 33 | cin.getline(a, 100); 34 | int lena = Length(a); 35 | if (lena > large_len) { 36 | Copy(largest, a); 37 | large_len = lena; 38 | } 39 | } 40 | cout << "Largest len: " << large_len << endl; 41 | cout << "Largest string: " << largest << endl; 42 | 43 | 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /Lecture-11/NumberAndString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/NumberAndString -------------------------------------------------------------------------------- /Lecture-11/NumberAndString.cpp: -------------------------------------------------------------------------------- 1 | // NumberAndString.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | char a[100]; 8 | int n; 9 | cin >> n; 10 | cin.get(); 11 | cin.getline(a, 100); 12 | 13 | cout << "Number: " << n << endl; 14 | cout << "String: " << a << endl; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lecture-11/Pallindrome: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/Pallindrome -------------------------------------------------------------------------------- /Lecture-11/Pallindrome.cpp: -------------------------------------------------------------------------------- 1 | // Pallindrome.cpp 2 | #include 3 | using namespace std; 4 | 5 | int Length(char *a) { 6 | int cnt = 0; 7 | 8 | for (int i = 0; a[i] != '\0' ; ++i) 9 | { 10 | cnt++; 11 | } 12 | return cnt; 13 | } 14 | 15 | bool isPallindrome(char *a) { 16 | int i = 0, j = Length(a) - 1; 17 | while (i < j) { 18 | if (a[i] != a[j]) { 19 | return false; 20 | } 21 | i++; 22 | j--; 23 | } 24 | return true; 25 | } 26 | 27 | int main() { 28 | 29 | char a[] = "MADAM"; 30 | 31 | if (isPallindrome(a)) { 32 | cout << "Pallindrome Hai\n"; 33 | } 34 | else { 35 | cout << "Pallindrome Nhi Hai\n"; 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Lecture-11/StringAppend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/StringAppend -------------------------------------------------------------------------------- /Lecture-11/StringAppend.cpp: -------------------------------------------------------------------------------- 1 | // StringAppend.cpp 2 | #include 3 | using namespace std; 4 | 5 | int Length(char *a) { 6 | int cnt = 0; 7 | 8 | for (int i = 0; a[i] != '\0' ; ++i) 9 | { 10 | cnt++; 11 | } 12 | return cnt; 13 | } 14 | 15 | void append(char *a, char *b) { 16 | int lena = Length(a); 17 | int lenb = Length(b); 18 | 19 | int i = lena, j = 0; 20 | while (j <= lenb) { 21 | a[i++] = b[j++]; 22 | } 23 | } 24 | 25 | int main() { 26 | 27 | char a[100] = "Hello"; 28 | char b[100] = "World"; 29 | 30 | cout << a << endl; 31 | cout << b << endl; 32 | append(a, b); 33 | cout << a << endl; 34 | cout << b << endl; 35 | 36 | return 0; 37 | } 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Lecture-11/StringLength: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/StringLength -------------------------------------------------------------------------------- /Lecture-11/StringLength.cpp: -------------------------------------------------------------------------------- 1 | // StringLength.cpp 2 | #include 3 | using namespace std; 4 | 5 | int Length(char *a) { 6 | int cnt = 0; 7 | 8 | for (int i = 0; a[i] != '\0' ; ++i) 9 | { 10 | cnt++; 11 | } 12 | return cnt; 13 | } 14 | 15 | int main() { 16 | 17 | char a[] = "Hello World"; 18 | 19 | cout << Length(a) << endl; 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-11/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-11/Test -------------------------------------------------------------------------------- /Lecture-11/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | char a[] = "Hello World"; 7 | char b[12] = "Hello World"; 8 | cout << a << endl; 9 | cout << b << endl; 10 | 11 | for (int i = 0 ; a[i] != '\0' ; i++) { 12 | cout << a[i] << "-"; 13 | } 14 | cout << endl; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lecture-12/2DArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-12/2DArray -------------------------------------------------------------------------------- /Lecture-12/2DArray.cpp: -------------------------------------------------------------------------------- 1 | // 2DArray.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[][3] = { 8 | {1, 2, 3}, 9 | {4, 5, 6}, 10 | {7, 8, 9} 11 | }; 12 | int n = 3, m = 3; 13 | 14 | for (int r = 0; r < n; ++r) 15 | { 16 | for (int c = 0; c < m; ++c) 17 | { 18 | cout << a[r][c] << " "; 19 | } 20 | cout << endl; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-12/2DArrayInput: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-12/2DArrayInput -------------------------------------------------------------------------------- /Lecture-12/2DArrayInput.cpp: -------------------------------------------------------------------------------- 1 | // 2DArrayInput.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[100][100]; 8 | 9 | int n, m; 10 | cout << "Enter rows and cols: "; 11 | cin >> n >> m; 12 | 13 | int number = 1; 14 | for (int r = 0; r < n; ++r) 15 | { 16 | for (int c = 0; c < m; ++c) 17 | { 18 | // cin >> a[r][c]; 19 | a[r][c] = number++; 20 | } 21 | } 22 | 23 | for (int r = 0; r < n; ++r) 24 | { 25 | for (int c = 0; c < m; ++c) 26 | { 27 | cout << a[r][c] << " "; 28 | } 29 | cout << endl; 30 | } 31 | 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Lecture-12/PermutationOrNot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-12/PermutationOrNot -------------------------------------------------------------------------------- /Lecture-12/PermutationOrNot.cpp: -------------------------------------------------------------------------------- 1 | // PermutationOrNot.cpp 2 | #include 3 | using namespace std; 4 | 5 | bool isPermutation(char *a, char *b) { 6 | int freq[26] = {0}; 7 | 8 | // print small abcd just for the reference 9 | char ch = 'a'; 10 | while (ch <= 'z') { 11 | cout << ch << " "; 12 | 13 | ch = ch + 1; 14 | } 15 | cout << endl; 16 | 17 | // a string par iterate krke freq array 18 | // ko update karo 19 | for (int i = 0; a[i] != '\0'; ++i) 20 | { 21 | char ch = a[i]; 22 | int indx = ch - 'a'; 23 | freq[indx]++; 24 | } 25 | 26 | // for (int i = 0; i < 26; ++i) 27 | // { 28 | // cout << freq[i] << " "; 29 | // } 30 | // cout << endl; 31 | 32 | // b string par iterate krke freq array 33 | // ko decrease karo 34 | for (int i = 0; b[i] != '\0'; ++i) 35 | { 36 | char ch = b[i]; 37 | int indx = ch - 'a'; 38 | freq[indx]--; 39 | } 40 | 41 | // for (int i = 0; i < 26; ++i) 42 | // { 43 | // cout << freq[i] << " "; 44 | // } 45 | // cout << endl; 46 | 47 | for (int i = 0; i < 26; ++i) 48 | { 49 | if (freq[i] != 0) { 50 | return false; 51 | } 52 | } 53 | 54 | return true; 55 | } 56 | 57 | int main() { 58 | 59 | char a[] = "aabcdxyza"; 60 | char b[] = "baxcyzad"; 61 | 62 | if (isPermutation(a, b)) { 63 | cout << "Permutation Hai\n"; 64 | } 65 | else { 66 | cout << "Permutation Nhi Hai\n"; 67 | } 68 | 69 | 70 | return 0; 71 | } 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /Lecture-12/ReverseString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-12/ReverseString -------------------------------------------------------------------------------- /Lecture-12/ReverseString.cpp: -------------------------------------------------------------------------------- 1 | // ReverseString.cpp 2 | #include 3 | using namespace std; 4 | 5 | int Length(char *a) { 6 | int cnt = 0; 7 | for (int i = 0; a[i] != '\0'; ++i) 8 | { 9 | cnt ++; 10 | } 11 | return cnt; 12 | } 13 | 14 | int main() { 15 | 16 | char a[] = "Hello World"; 17 | int lena = Length(a); 18 | 19 | cout << a << endl; 20 | 21 | int i = 0, j = lena - 1; 22 | while (i < j) { 23 | swap(a[i], a[j]); 24 | i++; 25 | j--; 26 | } 27 | cout << a << endl; 28 | 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture-12/RotateString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-12/RotateString -------------------------------------------------------------------------------- /Lecture-12/RotateString.cpp: -------------------------------------------------------------------------------- 1 | // RotateString.cpp 2 | #include 3 | using namespace std; 4 | 5 | int Length(char *a) { 6 | int cnt = 0; 7 | for (int i = 0; a[i] != '\0'; ++i) 8 | { 9 | cnt ++; 10 | } 11 | return cnt; 12 | } 13 | 14 | void RotateString(char *a, int n) { 15 | int lena = Length(a); 16 | // 1. shift all characters n position ahead 17 | for (int i = lena - 1; i >= 0; --i) 18 | { 19 | a[i + n] = a[i]; 20 | } 21 | cout << a << endl; 22 | 23 | // 2. Bring last n characters to front 24 | for (int i = lena, j = 0; j < n ; i++, j++) { 25 | a[j] = a[i]; 26 | } 27 | cout << a << endl; 28 | 29 | // 3. Add '\0' at lena index 30 | a[lena] = '\0'; 31 | } 32 | 33 | 34 | int main() { 35 | 36 | char a[100] = "Coding"; 37 | cout << a << endl; 38 | // int n = 3003; 39 | // n = n % Length(a); // Always take mod of n with string len 40 | RotateString(a, n); 41 | cout << a << endl; 42 | 43 | 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /Lecture-12/Substrings: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-12/Substrings -------------------------------------------------------------------------------- /Lecture-12/Substrings.cpp: -------------------------------------------------------------------------------- 1 | // Substrings.cpp 2 | #include 3 | using namespace std; 4 | 5 | void Substrings(char *a) { 6 | 7 | for (int i = 0; a[i] != '\0'; ++i) 8 | { 9 | int j = i; 10 | while (a[j] != '\0') { 11 | 12 | for (int k = i; k <= j; ++k) 13 | { 14 | cout << a[k]; 15 | } 16 | cout << endl; 17 | 18 | j++; 19 | } 20 | } 21 | } 22 | 23 | int main() { 24 | 25 | char a[] = "abcd"; 26 | 27 | Substrings(a); 28 | 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture-12/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-12/Test -------------------------------------------------------------------------------- /Lecture-12/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | cout << "INTMAX" << INT_MAX << endl; 7 | int val = INT_MAX + 1; 8 | cout << "INTMAX + 1 " << val << endl; 9 | cout << "INTMIN " << INT_MIN << endl; 10 | cout << "INTMIN - 1 " << INT_MIN - 1 << endl; 11 | 12 | return 0; 13 | } 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Lecture-13/FunctionsCharacter: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/FunctionsCharacter -------------------------------------------------------------------------------- /Lecture-13/FunctionsCharacter.cpp: -------------------------------------------------------------------------------- 1 | // FunctionsCharacter.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | char a[100] = "pro", b[100] = "procoder"; 9 | // cin.getline(a, 100); 10 | // cin.getline(b, 100); 11 | 12 | 13 | // Inbuilt functions 14 | cout << strlen(a) << endl; 15 | // cout << "a: " << a << endl; 16 | // cout << "b: " << b << endl; 17 | // strcat(a, b); // a = a + b; 18 | 19 | // cout << "a: " << a << endl; 20 | // cout << "b: " << b << endl; 21 | 22 | // Compare two strings 23 | // cout << strcmp(a, b) << endl; 24 | if (strcmp(a, b) == 0) 25 | { 26 | cout << a << " equals " << b << endl; 27 | } 28 | else if (strcmp(a, b) > 0) { 29 | cout << a << " is greater than " << b << endl; 30 | } 31 | else { 32 | cout << a << " is smaller than " << b << endl; 33 | } 34 | 35 | // 1. if (a < b) o/p < 0 36 | // 2. if (a>b) o/p > 0 37 | // 3. if(a==b) o/p == 0 38 | 39 | 40 | 41 | return 0; 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Lecture-13/SortStrings: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/SortStrings -------------------------------------------------------------------------------- /Lecture-13/SortStrings.cpp: -------------------------------------------------------------------------------- 1 | // SortStrings.cpp 2 | #include 3 | #include 4 | #include // sort 5 | using namespace std; 6 | 7 | bool meraCompare(string a, string b) { 8 | // cout << "Comparing: " << a << ", " << b << endl; 9 | if (a.length() == b.length()) return a < b; 10 | return a.length() < b.length(); 11 | } 12 | 13 | int main() { 14 | 15 | string s[100] = { 16 | "pineapple", 17 | "apple", 18 | "kiwi", 19 | "tivi", 20 | "fivi", 21 | "jivi", 22 | "papaya", 23 | "orange", 24 | "mango" 25 | }; 26 | int n = 9; 27 | for (int i = 0; i < n; ++i) 28 | { 29 | cout << s[i] << "\n"; 30 | } 31 | cout << "After Sorting: " << endl; 32 | sort(s, s + n, meraCompare); 33 | 34 | for (int i = 0; i < n; ++i) 35 | { 36 | cout << s[i] << "\n"; 37 | } 38 | 39 | /* 40 | int a[] = {1, 2, 3, 4, 0}; 41 | int n = sizeof(a) / sizeof(int); 42 | 43 | for (int i = 0; i < n; ++i) 44 | { 45 | cout << a[i] << " "; 46 | } 47 | cout << endl; 48 | 49 | sort(a, a + n, meraCompare); 50 | 51 | for (int i = 0; i < n; ++i) 52 | { 53 | cout << a[i] << " "; 54 | } 55 | cout << endl; 56 | */ 57 | 58 | 59 | return 0; 60 | } 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Lecture-13/SpiralPrint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/SpiralPrint -------------------------------------------------------------------------------- /Lecture-13/SpiralPrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void SpiralPrint(int a[][1000], int n, int m) { 5 | int sr, er, sc, ec; 6 | sr = sc = 0; 7 | er = n - 1; 8 | ec = m - 1; 9 | int cnt = 0; 10 | 11 | while (sr <= er and sc <= ec) { 12 | // 1. Print sr from sc to ec 13 | for (int col = sc; col <= ec; ++col) 14 | { 15 | cout << a[sr][col] << ", "; 16 | if (++cnt >= m * n) return; 17 | } 18 | sr++; 19 | 20 | // 2. Print ec from sr to er 21 | for (int row = sr; row <= er; ++row) 22 | { 23 | cout << a[row][ec] << ", "; 24 | if (++cnt >= m * n) return; 25 | } 26 | ec--; 27 | 28 | // 3. Print er from ec to sc 29 | if (sr < er) { 30 | for (int col = ec; col >= sc; --col) 31 | { 32 | cout << a[er][col] << ", "; 33 | if (++cnt >= m * n) return; 34 | 35 | } 36 | er--; 37 | } 38 | 39 | // 4. Print sc from er to sr 40 | if (sc < ec) { 41 | for (int row = er; row >= sr; --row) 42 | { 43 | cout << a[row][sc] << ", "; 44 | if (++cnt >= m * n) return; 45 | 46 | } 47 | sc++; 48 | } 49 | } 50 | } 51 | 52 | int main() { 53 | 54 | 55 | int arr[1000][1000]; 56 | int rows, cols; 57 | cin >> rows >> cols; //4 4 58 | 59 | // input array 60 | 61 | for (int i = 0; i <= rows - 1; i++) { 62 | for (int j = 0; j <= cols - 1; j++) { 63 | cin >> arr[i][j]; 64 | } 65 | } 66 | 67 | SpiralPrint(arr, rows, cols); 68 | cout << "END\n"; 69 | 70 | 71 | 72 | return 0; 73 | } -------------------------------------------------------------------------------- /Lecture-13/String: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/String -------------------------------------------------------------------------------- /Lecture-13/String-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/String-1 -------------------------------------------------------------------------------- /Lecture-13/String-1.cpp: -------------------------------------------------------------------------------- 1 | // String - 1.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | string s = ""; 8 | s.push_back('A'); 9 | s.push_back('B'); 10 | s.push_back('C'); 11 | s.push_back('D'); 12 | 13 | cout << s << endl; 14 | // s.pop_back(); 15 | 16 | // cout << s << endl; 17 | 18 | for (int i = 0; i < s.length(); ++i) 19 | { 20 | cout << s[i] << " "; 21 | } 22 | cout << endl; 23 | 24 | 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Lecture-13/String.cpp: -------------------------------------------------------------------------------- 1 | // String.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | string s, s1; 8 | s = "Zello World"; 9 | cout << s << endl; 10 | 11 | s1 = "Learning Strings"; 12 | 13 | if (s > s1) { 14 | cout << s << " is greater than " << s1 << endl; 15 | } 16 | else if (s < s1) { 17 | cout << s << " is smaller than " << s1 << endl; 18 | } 19 | else { 20 | cout << s << " is equals to " << s1 << endl; 21 | } 22 | 23 | s += s1; 24 | cout << s << endl; 25 | getline(cin, s, '$'); // To take whitespaces input 26 | cout << s << endl; 27 | 28 | /* 29 | cout << s1 << endl; 30 | cout << "Enter s: "; 31 | cin >> s; // cin ignores whitespaces 32 | cout << s << endl; 33 | 34 | cin.ignore(); 35 | cout << "Enter string: "; 36 | getline(cin, s,'$'); // To take whitespaces input 37 | cout << s << endl; 38 | */ 39 | 40 | 41 | return 0; 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Lecture-13/StringArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/StringArray -------------------------------------------------------------------------------- /Lecture-13/StringArray.cpp: -------------------------------------------------------------------------------- 1 | // StringArray.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | string s[100]; 8 | int n; 9 | cin >> n; 10 | cin.get(); 11 | 12 | for (int i = 0; i < n; ++i) 13 | { 14 | getline(cin, s[i]); 15 | } 16 | 17 | for (int i = 0; i < n; ++i) 18 | { 19 | cout << s[i] << "\n"; 20 | } 21 | 22 | for (int i = 0; i < n; ++i) 23 | { 24 | for (int j = 0; j < s[i].length(); ++j) 25 | { 26 | cout << s[i][j] << " "; 27 | } 28 | cout << endl; 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lecture-13/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/Test -------------------------------------------------------------------------------- /Lecture-13/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[][4] = { 7 | {1, 2, 5, 4}, 8 | {3, 7, 6, 8}, 9 | {10, 11, 23, 25}, 10 | {100, 19, 20, 21} 11 | }; 12 | int n, m, key = 19; 13 | n = m = 4; 14 | bool kyaMilli = false; 15 | for (int i = 0; i < n; ++i) 16 | { 17 | for (int j = 0; j < m; ++j) 18 | { 19 | if (a[i][j] == key) { 20 | cout << "Key found at: " << i << ", " << j << endl; 21 | kyaMilli = true; 22 | break; 23 | } 24 | } 25 | if (kyaMilli == true) { 26 | break; 27 | } 28 | } 29 | if (kyaMilli == false) { 30 | cout << "Key not found\n"; 31 | } 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Lecture-13/WavePrint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-13/WavePrint -------------------------------------------------------------------------------- /Lecture-13/WavePrint.cpp: -------------------------------------------------------------------------------- 1 | // WavePrint.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[100][100]; 8 | int n = 4, m = 4, number = 1; 9 | 10 | for (int i = 0; i < n; ++i) 11 | { 12 | for (int j = 0; j < m; ++j) 13 | { 14 | a[i][j] = number++; 15 | } 16 | } 17 | 18 | for (int i = 0; i < n; ++i) 19 | { 20 | for (int j = 0; j < m; ++j) 21 | { 22 | cout << a[i][j] << " "; 23 | } 24 | cout << endl; 25 | } 26 | 27 | for (int col = 0; col < m; ++col) 28 | { 29 | if (col % 2 == 0) { 30 | for (int row = 0; row < n; ++row) 31 | { 32 | cout << a[row][col] << " "; 33 | } 34 | } 35 | else { 36 | for (int row = n - 1; row >= 0; --row) 37 | { 38 | cout << a[row][col] << " "; 39 | } 40 | } 41 | } 42 | cout << endl; 43 | 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /Lecture-14/Factorial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-14/Factorial -------------------------------------------------------------------------------- /Lecture-14/Factorial.cpp: -------------------------------------------------------------------------------- 1 | // Factorial.cpp 2 | #include 3 | using namespace std; 4 | 5 | int fact(int n) { 6 | // 1. Base case 7 | if (n == 0) { 8 | return 1; 9 | } 10 | 11 | return n * fact(n - 1); 12 | } 13 | 14 | int main() { 15 | int n; 16 | cin >> n; 17 | 18 | cout << fact(n) << endl; 19 | 20 | return 0; 21 | } 22 | /* 23 | int factorial(int n) { 24 | // 1. Base case 25 | if (n == 0) { 26 | return 1; 27 | } 28 | 29 | // 2. Recurrence relation, fact(n) = n*fact(n-1); 30 | // Hey recursion hume chota n-1 ka factorial de do, please(Blind Trust) 31 | int chotaFactorial = factorial(n - 1); // Choti problem solve kari recursion ne 32 | int badaFactorial = n * chotaFactorial; 33 | 34 | return badaFactorial; 35 | } 36 | */ 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Lecture-14/Fibonacci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-14/Fibonacci -------------------------------------------------------------------------------- /Lecture-14/Fibonacci.cpp: -------------------------------------------------------------------------------- 1 | // Fibonacci.cpp 2 | #include 3 | using namespace std; 4 | 5 | int fibo(int n) { 6 | // base case 7 | if (n == 0 || n == 1) { 8 | return n; 9 | } 10 | // recursive case 11 | return fibo(n - 1) + fibo(n - 2); 12 | } 13 | 14 | int main() { 15 | 16 | int n; 17 | cin >> n; 18 | 19 | cout << fibo(n) << endl; 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-14/PrintDecreasing: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-14/PrintDecreasing -------------------------------------------------------------------------------- /Lecture-14/PrintDecreasing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printDecreasing(int n) { 5 | // base case 6 | if (n == 0) { 7 | return; 8 | } 9 | 10 | // recursive case 11 | printDecreasing(n - 1); 12 | cout << n << " "; 13 | } 14 | 15 | int main() { 16 | 17 | int n; 18 | cin >> n; 19 | 20 | printDecreasing(n); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Lecture-14/SumOfNNumbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-14/SumOfNNumbers -------------------------------------------------------------------------------- /Lecture-14/SumOfNNumbers.cpp: -------------------------------------------------------------------------------- 1 | // SumOfNNumbers.cpp 2 | #include 3 | using namespace std; 4 | 5 | int Sum(int n) { 6 | // base case 7 | if (n == 0) { 8 | return 0; 9 | } 10 | 11 | // recursive case, Sum(n) = n + Sum(n-1) 12 | int ChotaSum = Sum(n - 1); // Hey recursion please dede answer, Assumption(Blind trust) 13 | int BadaSum = n + ChotaSum; 14 | 15 | return BadaSum; 16 | } 17 | 18 | int Sum(int n) { 19 | // base case 20 | if (n == 0) { 21 | return 0; 22 | } 23 | 24 | return Sum(n - 1) + n; 25 | } 26 | 27 | int main() { 28 | 29 | int n; 30 | cin >> n; 31 | 32 | cout << Sum(n) << endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Lecture-14/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Lecture-15/ArraySorted: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-15/ArraySorted -------------------------------------------------------------------------------- /Lecture-15/ArraySorted.cpp: -------------------------------------------------------------------------------- 1 | // ArraySorted.cpp 2 | #include 3 | using namespace std; 4 | 5 | bool isArraySorted(int *a, int n) { 6 | // base case 7 | if (n <= 1) { 8 | return true; 9 | } 10 | // recursive case 11 | bool kyaChotaSortedHai = isArraySorted(a + 1, n - 1); 12 | if (a[0] <= a[1] and kyaChotaSortedHai == true) { 13 | return true; 14 | } 15 | return false; 16 | } 17 | 18 | bool isArraySorted1(int *a, int n) { 19 | // base case 20 | if (n <= 1) { 21 | return true; 22 | } 23 | // recursive case 24 | bool kyaChotaSortedHai = isArraySorted1(a, n - 1); 25 | if (a[n - 1] >= a[n - 2] and kyaChotaSortedHai == true) { 26 | return true; 27 | } 28 | return false; 29 | } 30 | 31 | bool isArraySorted2(int *a, int n, int i) { 32 | // base case 33 | if (i == n - 1) { 34 | return true; 35 | } 36 | 37 | // recursive case 38 | bool kyaChotaSortedHai = isArraySorted2(a, n, i + 1); 39 | if (a[i] <= a[i + 1] and kyaChotaSortedHai == true) { 40 | return true; 41 | } 42 | else { 43 | return false; 44 | } 45 | } 46 | 47 | int main() { 48 | 49 | int a[] = {1, 3, 4, 5, 6, 7}; 50 | int n = sizeof(a) / sizeof(int); 51 | 52 | 53 | if (isArraySorted2(a, n, 0)) { 54 | cout << "Sorted hai\n"; 55 | } 56 | else { 57 | cout << "Sorted nahi hai\n"; 58 | } 59 | 60 | 61 | 62 | return 0; 63 | } 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Lecture-15/Check7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-15/Check7 -------------------------------------------------------------------------------- /Lecture-15/Check7.cpp: -------------------------------------------------------------------------------- 1 | // Check7.cpp 2 | #include 3 | using namespace std; 4 | 5 | bool Check7(int *a, int n) { 6 | // base case 7 | if (n == 0) { 8 | return false; 9 | } 10 | 11 | // recursive case 12 | if (a[0] == 7) { 13 | return true; 14 | } 15 | return Check7(a + 1, n - 1); 16 | } 17 | 18 | 19 | int main() { 20 | 21 | int a[] = {1, 2, 3, 17, 71, 6, 4, 5}; 22 | int n = sizeof(a) / sizeof(int); 23 | 24 | if (Check7(a, n)) { 25 | cout << "7 is present\n"; 26 | } 27 | else { 28 | cout << "7 is not present\n"; 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lecture-15/First7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-15/First7 -------------------------------------------------------------------------------- /Lecture-15/First7.cpp: -------------------------------------------------------------------------------- 1 | // First7.cpp 2 | #include 3 | using namespace std; 4 | 5 | int first7(int *a, int n) { 6 | // base case 7 | if (n == 0) { 8 | return -1; 9 | } 10 | 11 | // recursive case 12 | if (a[0] == 7) { 13 | return 0; 14 | } 15 | 16 | int ans = first7(a + 1, n - 1); 17 | if (ans == -1) { 18 | return -1; 19 | } 20 | return ans + 1; 21 | } 22 | 23 | int pehla7(int *a, int n, int i) { 24 | // base case 25 | if (i == n) { 26 | return -1; 27 | } 28 | // recursive case 29 | if (a[i] == 7) { 30 | return i; 31 | } 32 | 33 | int ans = pehla7(a, n, i + 1); 34 | return ans; 35 | } 36 | 37 | int main() { 38 | 39 | int a[] = {1, 2, 3, 17, 7, 4, 5}; 40 | int n = sizeof(a) / sizeof(int); 41 | 42 | cout << first7(a, n) << endl; 43 | cout << pehla7(a, n, 0) << endl; 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /Lecture-15/IntToString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-15/IntToString -------------------------------------------------------------------------------- /Lecture-15/IntToString.cpp: -------------------------------------------------------------------------------- 1 | // IntToString.cpp 2 | #include 3 | using namespace std; 4 | 5 | string s[10] = { 6 | "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" 7 | }; 8 | 9 | void IntToString(int n) { 10 | // base case 11 | if (n == 0) { 12 | return; 13 | } 14 | 15 | int last_digit = n % 10; 16 | IntToString(n / 10); 17 | cout << s[last_digit] << " "; 18 | } 19 | 20 | int main() { 21 | 22 | int n = 2048; 23 | IntToString(n); 24 | 25 | cout << endl; 26 | 27 | return 0; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Lecture-15/SumOfArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-15/SumOfArray -------------------------------------------------------------------------------- /Lecture-15/SumOfArray.cpp: -------------------------------------------------------------------------------- 1 | // SumOfArray.cpp 2 | #include 3 | using namespace std; 4 | 5 | int SumOfArray(int *a, int n) { 6 | // base case 7 | if (n == 0) { 8 | return 0; 9 | } 10 | 11 | // recursive case 12 | return a[0] + SumOfArray(a + 1, n - 1); 13 | } 14 | 15 | int main() { 16 | 17 | int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 18 | int n = sizeof(a) / sizeof(int); 19 | 20 | 21 | cout << SumOfArray(a, n) << endl; 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-15/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Lecture-16/BinarySearch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-16/BinarySearch -------------------------------------------------------------------------------- /Lecture-16/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | // BinarySearch.cpp 2 | #include 3 | using namespace std; 4 | 5 | int BinarySearch(int *a, int s, int e, int key) { 6 | // base case 7 | if (s > e) { 8 | return -1; 9 | } 10 | // recursive case 11 | int mid = (s + e) / 2; 12 | if (a[mid] == key) { 13 | return mid; 14 | } 15 | else if (a[mid] < key) { 16 | return BinarySearch(a, mid + 1, e, key); 17 | } 18 | else { 19 | return BinarySearch(a, s, mid - 1, key); 20 | } 21 | } 22 | 23 | int main() { 24 | 25 | int a[] = {1, 2, 3, 4, 5}; 26 | int n = sizeof(a) / sizeof(int); 27 | 28 | cout << BinarySearch(a, 0, n - 1, 1) << endl; 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture-16/BubbleSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-16/BubbleSort -------------------------------------------------------------------------------- /Lecture-16/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | // BubbleSort.cpp 2 | #include 3 | using namespace std; 4 | 5 | void BubbleSort(int *a, int n, int i) { 6 | // base case 7 | if (i == n - 1) { 8 | return; 9 | } 10 | 11 | // recursive case 12 | for (int j = 0; j < n - 1 - i; ++j) 13 | { 14 | if (a[j] > a[j + 1]) { 15 | swap(a[j], a[j + 1]); 16 | } 17 | } 18 | BubbleSort(a, n, i + 1); 19 | } 20 | void BubbleSort(int *a, int n, int i, int j) { 21 | // base case 22 | if (i == n - 1) { 23 | return; 24 | } 25 | 26 | // recursive case 27 | if (j < n - 1 - i) { 28 | if (a[j] > a[j + 1]) { 29 | swap(a[j], a[j + 1]); 30 | } 31 | BubbleSort(a, n, i, j + 1); 32 | } 33 | else { 34 | BubbleSort(a, n, i + 1, 0); 35 | } 36 | } 37 | 38 | int main() { 39 | 40 | int a[] = {5, 4, 3, 2, 1, 0, -1}; 41 | int n = sizeof(a) / sizeof(int); 42 | 43 | for (int i = 0; i < n; ++i) 44 | { 45 | cout << a[i] << " "; 46 | } cout << endl; 47 | 48 | BubbleSort(a, n, 0, 0); 49 | 50 | for (int i = 0; i < n; ++i) 51 | { 52 | cout << a[i] << " "; 53 | } cout << endl; 54 | 55 | return 0; 56 | } 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /Lecture-16/GUNGUN: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-16/GUNGUN -------------------------------------------------------------------------------- /Lecture-16/GUNGUN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // int a[][3] = { 7 | // {1, 2, 3}, 8 | // {4, 5, 6}, 9 | // {7, 8, 9} 10 | // }; 11 | 12 | // int n = 3, m = 3; 13 | 14 | int a[100][100]; 15 | int n, m; 16 | cin >> n >> m; 17 | 18 | // for (int r = 0; r < n; ++r) 19 | // { 20 | // for (int c = 0; c < m; ++c) 21 | // { 22 | // cin >> a[r][c]; 23 | // } 24 | // } 25 | int number = 1; 26 | for (int r = 0; r < n; ++r) 27 | { 28 | for (int c = 0; c < m; ++c) 29 | { 30 | a[r][c] = number++; 31 | } 32 | } 33 | int key; 34 | cout << "Enter key: "; 35 | cin >> key; 36 | bool keyMili = false; 37 | 38 | for (int r = 0; r < n; ++r) 39 | { 40 | for (int c = 0; c < m; ++c) 41 | { 42 | // cout << a[r][c] << " "; 43 | if (a[r][c] == key) { 44 | keyMili = true; 45 | cout << "Key found\n"; 46 | } 47 | } 48 | // cout << endl; 49 | } 50 | if (keyMili == false) { 51 | cout << "Key not found\n"; 52 | } 53 | 54 | return 0; 55 | } 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Lecture-16/Last7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-16/Last7 -------------------------------------------------------------------------------- /Lecture-16/Last7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int last7(int *a, int n, int i) { 5 | // base case 6 | if (i == n) { 7 | return -1; 8 | } 9 | // recursive case 10 | if (a[i] == 7) { 11 | int ni = last7(a, n, i + 1); 12 | return max(i, ni); 13 | } 14 | else { 15 | return last7(a, n, i + 1); 16 | } 17 | } 18 | 19 | void allOccurances7(int *a, int n, int i) { 20 | // base case 21 | if (i == n) { 22 | return; 23 | } 24 | 25 | // recursive case 26 | if (a[i] == 7) { 27 | cout << i << " "; 28 | } 29 | allOccurances7(a, n, i + 1); 30 | } 31 | 32 | int main() { 33 | 34 | int a[] = {1, 2, 7, 7, 7, 7, 3, 4, 5}; 35 | int n = sizeof(a) / sizeof(int); 36 | 37 | cout << last7(a, n, 0) << endl; 38 | allOccurances7(a, n, 0); 39 | cout << endl; 40 | 41 | return 0; 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Lecture-16/MergeSortedArrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-16/MergeSortedArrays -------------------------------------------------------------------------------- /Lecture-16/MergeSortedArrays.cpp: -------------------------------------------------------------------------------- 1 | // MergeSortedArrays.cpp 2 | #include 3 | using namespace std; 4 | 5 | void merge(int *a, int *b, int *c, int n, int m) { 6 | int i = 0, j = 0, k = 0; 7 | while (i < n and j < m) { 8 | if (b[i] < c[j]) { 9 | a[k++] = b[i++]; 10 | } 11 | else { 12 | a[k++] = c[j++]; 13 | } 14 | } 15 | 16 | while (j < m) { 17 | a[k++] = c[j++]; 18 | } 19 | 20 | while (i < n) { 21 | a[k++] = b[i++]; 22 | } 23 | } 24 | 25 | void print(int *a, int n) { 26 | for (int i = 0; i < n; ++i) 27 | { 28 | cout << a[i] << " "; 29 | } 30 | cout << endl; 31 | } 32 | 33 | 34 | int main() { 35 | 36 | int a[100], b[] = {2, 4, 5, 7}, c[] = {1, 3, 6, 8, 9, 10}; 37 | int n = sizeof(b) / sizeof(int); 38 | int m = sizeof(c) / sizeof(int); 39 | 40 | merge(a, b, c, n, m); 41 | 42 | print(b, n); 43 | print(c, m); 44 | print(a, m + n); 45 | 46 | 47 | 48 | return 0; 49 | } 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /Lecture-16/Multiply: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-16/Multiply -------------------------------------------------------------------------------- /Lecture-16/Multiply.cpp: -------------------------------------------------------------------------------- 1 | // Multiply.cpp 2 | #include 3 | using namespace std; 4 | 5 | int multiply(int a, int b) { 6 | // base case 7 | if (b == 0) { 8 | return 0; 9 | } 10 | 11 | // recursive case 12 | return a + multiply(a, b - 1); 13 | } 14 | 15 | int main() { 16 | 17 | cout << multiply(57, 373) << endl; 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lecture-16/StringToInt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-16/StringToInt -------------------------------------------------------------------------------- /Lecture-16/StringToInt.cpp: -------------------------------------------------------------------------------- 1 | // StringToInt.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int StringToInt(char *a, int n) { 7 | // base case 8 | if (n == 0) { 9 | return 0; 10 | } 11 | 12 | int ld = a[n - 1] - '0'; 13 | int ca = StringToInt(a, n - 1); 14 | return ca * 10 + ld; 15 | } 16 | 17 | int main() { 18 | 19 | char a[] = "1234"; 20 | int n = strlen(a); 21 | 22 | cout << StringToInt(a, n) << endl; 23 | 24 | return 0; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Lecture-17/MergeSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-17/MergeSort -------------------------------------------------------------------------------- /Lecture-17/MergeSort.cpp: -------------------------------------------------------------------------------- 1 | // MergeSort.cpp 2 | #include 3 | using namespace std; 4 | 5 | void merge(int *a, int *b, int *c, int s, int e) { 6 | int m = (s + e) / 2; 7 | int i = s, j = m + 1, k = s; 8 | 9 | while (i <= m and j <= e) { 10 | if (b[i] < c[j]) { 11 | a[k++] = b[i++]; 12 | } 13 | else { 14 | a[k++] = c[j++]; 15 | } 16 | } 17 | 18 | while (j <= e) { 19 | a[k++] = c[j++]; 20 | } 21 | 22 | while (i <= m) { 23 | a[k++] = b[i++]; 24 | } 25 | } 26 | 27 | void mergeSort(int *a, int s, int e) { 28 | // base case 29 | if (s >= e) { 30 | return ; 31 | } 32 | // recursive case 33 | // 1. Divide 34 | int b[1000], c[1000]; 35 | int m = (s + e) / 2; 36 | for (int i = s ; i <= m ; i++) { 37 | b[i] = a[i]; 38 | } 39 | for (int i = m + 1; i <= e; i++) { 40 | c[i] = a[i]; 41 | } 42 | // 2. Sort 43 | mergeSort(b, s, m); 44 | mergeSort(c, m + 1, e); 45 | 46 | // 3. Merge 47 | merge(a, b, c, s, e); 48 | } 49 | 50 | int main() { 51 | 52 | int a[] = {5, 4, 3, 2, 1, 0}; 53 | int n = sizeof(a) / sizeof(int); 54 | 55 | for (int i = 0; i < n; ++i) 56 | { 57 | cout << a[i] << " "; 58 | } 59 | cout << endl; 60 | 61 | mergeSort(a, 0, n - 1); 62 | 63 | for (int i = 0; i < n; ++i) 64 | { 65 | cout << a[i] << " "; 66 | } 67 | cout << endl; 68 | 69 | return 0; 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /Lecture-17/Permutation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-17/Permutation -------------------------------------------------------------------------------- /Lecture-17/Permutation.cpp: -------------------------------------------------------------------------------- 1 | // Permutation.cpp 2 | #include 3 | using namespace std; 4 | 5 | void Permutation(char *a, int i) { 6 | // base case 7 | if (a[i] == '\0') { 8 | cout << a << endl; 9 | return; 10 | } 11 | 12 | // recursive case 13 | for (int j = i; a[j] != '\0'; ++j) 14 | { 15 | swap(a[j], a[i]); 16 | Permutation(a, i + 1); 17 | swap(a[j], a[i]); 18 | 19 | } 20 | } 21 | 22 | int main() { 23 | 24 | char a[] = "abc"; 25 | 26 | Permutation(a, 0); 27 | 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Lecture-17/Subsequences: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-17/Subsequences -------------------------------------------------------------------------------- /Lecture-17/Subsequences.cpp: -------------------------------------------------------------------------------- 1 | // Subsequences.cpp 2 | #include 3 | using namespace std; 4 | 5 | void Subsequences(char *in, char*out, int i, int j) { 6 | // base case 7 | if (in[i] == '\0') { 8 | out[j] = '\0'; 9 | cout << out << endl; 10 | return; 11 | } 12 | 13 | // recursive case 14 | // 1. ith character ko output mei mat lo 15 | Subsequences(in, out, i + 1, j); 16 | 17 | // 2. ith character ko output mei le lo 18 | out[j] = in[i]; 19 | Subsequences(in, out, i + 1, j + 1); 20 | } 21 | 22 | int main() { 23 | 24 | char in[100] = "abc"; 25 | char out[100]; 26 | 27 | Subsequences(in, out, 0, 0); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Lecture-17/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Lecture-17/toh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-17/toh -------------------------------------------------------------------------------- /Lecture-17/toh.cpp: -------------------------------------------------------------------------------- 1 | // toh.cpp 2 | #include 3 | using namespace std; 4 | 5 | void toh(int n, char src, char helper, char des) { 6 | // base case 7 | if (n == 0) { 8 | return; 9 | } 10 | 11 | // recursive case 12 | toh(n - 1, src, des, helper); 13 | cout << "Taking disk " << n << " from " << src << " to " << des << endl; 14 | toh(n - 1, helper, src, des); 15 | } 16 | 17 | int main() { 18 | int n; 19 | cin >> n; 20 | 21 | toh(n, 'A', 'B', 'C'); 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-19/RatInMaze: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-19/RatInMaze -------------------------------------------------------------------------------- /Lecture-19/RatInMaze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool RatInMaze(char maze[][5], int sol[][10], int i, int j, int n, int m) { 5 | // base case 6 | if (i == n - 1 and j == m - 1) { 7 | sol[i][j] = 1; 8 | for (int i = 0; i < n; ++i) 9 | { 10 | for (int j = 0; j < m; ++j) 11 | { 12 | cout << sol[i][j] << " "; 13 | } 14 | cout << endl; 15 | } 16 | cout << endl; 17 | return false; 18 | } 19 | 20 | // recursive case 21 | sol[i][j] = 1; // Ho sakta hai i,j solution ka part ho 22 | 23 | // 1. Check right se raasta milla? --> (i,j+1) 24 | if (j + 1 < m and maze[i][j + 1] != 'X') { 25 | bool kyaRightSeRaastaMilla = RatInMaze(maze, sol, i, j + 1, n, m); 26 | if (kyaRightSeRaastaMilla == true) { 27 | return true; 28 | } 29 | } 30 | // 2. Check neeche se raasta mila? --> (i+1,j) 31 | if (i + 1 < n and maze[i + 1][j] != 'X') { 32 | bool kyaNeecheSeRaastaMilla = RatInMaze(maze, sol, i + 1, j, n, m); 33 | if (kyaNeecheSeRaastaMilla == true) { 34 | return true; 35 | } 36 | } 37 | 38 | sol[i][j] = 0; 39 | return false; 40 | } 41 | 42 | int main() { 43 | 44 | char maze[][5] = { 45 | "0000", 46 | "00XX", 47 | "0000", 48 | "XX00" 49 | }; 50 | int sol[10][10] = {0}; 51 | 52 | RatInMaze(maze, sol, 0, 0, 4, 4); 53 | 54 | 55 | 56 | return 0; 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /Lecture-19/RatInMazeAdvanced: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-19/RatInMazeAdvanced -------------------------------------------------------------------------------- /Lecture-2/CheckPrime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/CheckPrime -------------------------------------------------------------------------------- /Lecture-2/CheckPrime.cpp: -------------------------------------------------------------------------------- 1 | // CheckPrime.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, i; 8 | cin >> n; 9 | 10 | i = 2; 11 | while (i <= n - 1) { 12 | if (n % i == 0) { 13 | cout << "Not prime" << endl; 14 | return 0; 15 | } 16 | 17 | i = i + 1; 18 | } 19 | cout << "Prime" << endl; 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-2/Conditionals: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/Conditionals -------------------------------------------------------------------------------- /Lecture-2/Conditionals.cpp: -------------------------------------------------------------------------------- 1 | // Conditionals.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a; 8 | cin >> a; 9 | 10 | if (a > 10 && a < 20) { 11 | cout << "Hello World" << '\n'; 12 | } 13 | else if (a > 20 && a < 30) { 14 | cout << "Welcome to the world\n"; 15 | } 16 | else { 17 | cout << "Okay" << endl; 18 | } 19 | 20 | // if (a > 10) { 21 | // cout << "Hello World" << '\n'; 22 | // } 23 | // else { 24 | // cout << "Welcome to the World\n"; 25 | // } 26 | 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Lecture-2/LargestOfN: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/LargestOfN -------------------------------------------------------------------------------- /Lecture-2/LargestOfN.cpp: -------------------------------------------------------------------------------- 1 | // LargestOfN.cpp 2 | #include 3 | #include // INT_MAX: 2^31 - 1, INT_MIN: -2^31 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | int n, i, no, largest; 9 | cin >> n; 10 | 11 | // Taking n times input and printing the result 12 | i = 1; 13 | largest = INT_MIN; 14 | while (i <= n) { 15 | cin >> no; 16 | 17 | // cout << "User entered: " << no << endl; 18 | if (no > largest) { 19 | largest = no; 20 | } 21 | 22 | i = i + 1; 23 | } 24 | 25 | cout << "Largest number: " << largest << endl; 26 | 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Lecture-2/Pattern123: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/Pattern123 -------------------------------------------------------------------------------- /Lecture-2/Pattern123.cpp: -------------------------------------------------------------------------------- 1 | // Pattern123.cpp 2 | // PatternStar.cpp 3 | #include 4 | using namespace std; 5 | /* 6 | Print the following pattern: N = 4, 7 | 1 8 | 2 3 9 | 4 5 6 10 | 7 8 9 10 11 | */ 12 | int main() { 13 | int n, i; 14 | cin >> n; 15 | 16 | int row = 1, no = 1; 17 | 18 | while (row <= n) { 19 | // Work 20 | // har row mei, row times star print karne hai 21 | i = 1; 22 | while (i <= row) { 23 | cout << no << " "; 24 | no = no + 1; 25 | i = i + 1; 26 | } 27 | 28 | cout << '\n'; 29 | row = row + 1; 30 | } 31 | 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Lecture-2/PatternStar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/PatternStar -------------------------------------------------------------------------------- /Lecture-2/PatternStar.cpp: -------------------------------------------------------------------------------- 1 | // PatternStar.cpp 2 | #include 3 | using namespace std; 4 | /* 5 | Print the following pattern: N = 4, 6 | * 7 | * * 8 | * * * 9 | * * * * 10 | 11 | */ 12 | int main() { 13 | int n, i; 14 | cin >> n; 15 | 16 | int row = 1; 17 | 18 | while (row <= n) { 19 | // Work 20 | // har row mei, row times star print karne hai 21 | i = 1; 22 | while (i <= row) { 23 | cout << '*' << " "; 24 | i = i + 1; 25 | } 26 | 27 | cout << '\n'; 28 | row = row + 1; 29 | } 30 | 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Lecture-2/PrintAllEven: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/PrintAllEven -------------------------------------------------------------------------------- /Lecture-2/PrintAllEven.cpp: -------------------------------------------------------------------------------- 1 | // PrintAllEven.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int i, n; 8 | cin >> n; 9 | 10 | i = 1;// Intialization 11 | while (i <= n) { // Condition Check 12 | // Work 13 | if (i % 2 == 0) { 14 | cout << i << ' '; 15 | } 16 | 17 | i = i + 1; // Updation 18 | } 19 | cout << endl; // endl == '\n' 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-2/SimpleInterest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/SimpleInterest -------------------------------------------------------------------------------- /Lecture-2/SimpleInterest.cpp: -------------------------------------------------------------------------------- 1 | // SimpleInterest.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int p, r, t; 8 | cin >> p >> r >> t; 9 | 10 | float si; 11 | 12 | si = (p * r * t) / 100.0; 13 | 14 | cout << "Simple Interest: " << si << endl; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lecture-2/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | 8 | return 0; 9 | } 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Lecture-2/WhileLoop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/WhileLoop -------------------------------------------------------------------------------- /Lecture-2/WhileLoop.cpp: -------------------------------------------------------------------------------- 1 | // WhileLoop.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int i, n; 8 | cin >> n; 9 | 10 | i = 1;// Intialization 11 | while (i <= n) { // Condition Check 12 | // Work 13 | cout << i << ' '; 14 | 15 | i = i + 1; // Updation 16 | } 17 | cout << endl; // endl == '\n' 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lecture-2/untitled: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-2/untitled -------------------------------------------------------------------------------- /Lecture-20/MazeWays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-20/MazeWays -------------------------------------------------------------------------------- /Lecture-20/MazeWays.cpp: -------------------------------------------------------------------------------- 1 | // MazeWays.cpp 2 | #include 3 | using namespace std; 4 | 5 | int MazeWays(int n, int m) { 6 | if (n == 0 and m == 0) { 7 | return 1; 8 | } 9 | 10 | if (n < 0 || m < 0) { 11 | return 0; 12 | } 13 | 14 | return MazeWays(n - 1, m) + MazeWays(n, m - 1); 15 | } 16 | 17 | int main() { 18 | 19 | int n = 3, m = 3; 20 | cout << MazeWays(n, m) << endl; 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Lecture-20/NStairCase: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-20/NStairCase -------------------------------------------------------------------------------- /Lecture-20/NStairCase.cpp: -------------------------------------------------------------------------------- 1 | // NStairCase.cpp 2 | #include 3 | using namespace std; 4 | 5 | int NStairCase(int n) { 6 | if (n == 0) { 7 | return 1; 8 | } 9 | if (n < 0) { 10 | return 0; 11 | } 12 | 13 | return NStairCase(n - 1) + NStairCase(n - 2) + NStairCase(n - 3); 14 | } 15 | 16 | int NStairs(int n, int k) { 17 | if (n == 0) { 18 | return 1; 19 | } 20 | if (n < 0) { 21 | return 0; 22 | } 23 | int ans = 0; 24 | for (int i = 1; i <= k; ++i) 25 | { 26 | ans += NStairs(n - i, k); 27 | } 28 | return ans; 29 | } 30 | 31 | int main() { 32 | 33 | int n; 34 | cin >> n; 35 | cout << NStairCase(n) << endl; 36 | cout << NStairs(n, 3) << endl; 37 | 38 | return 0; 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Lecture-20/SudokuSolver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-20/SudokuSolver -------------------------------------------------------------------------------- /Lecture-20/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-20/Test -------------------------------------------------------------------------------- /Lecture-21/2DVector: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-21/2DVector -------------------------------------------------------------------------------- /Lecture-21/2DVector-Array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-21/2DVector-Array -------------------------------------------------------------------------------- /Lecture-21/2DVector-Array.cpp: -------------------------------------------------------------------------------- 1 | // 2DVector - Array.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | // vector< vector > v(rows, vector(cols, initial_value)); 9 | int rows, cols; 10 | cin >> rows >> cols; 11 | 12 | vector > v(rows, vector(cols)); 13 | int number = 1; 14 | for (int i = 0; i < rows; ++i) 15 | { 16 | for (int j = 0; j < cols; ++j) 17 | { 18 | v[i][j] = number++; 19 | } 20 | } 21 | 22 | for (int i = 0; i < rows; ++i) 23 | { 24 | for (int j = 0; j < cols; ++j) 25 | { 26 | cout << v[i][j] << " "; 27 | } 28 | cout << endl; 29 | } 30 | 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Lecture-21/2DVector.cpp: -------------------------------------------------------------------------------- 1 | // 2DVector.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | vector > v; 9 | 10 | vector v1({1, 2, 3, 4}); 11 | vector v2({1, 2, 3, 4, 5, 6}); 12 | vector v3({1, 2}); 13 | vector v4({1, 2, 9, 10, 11, 12, 13}); 14 | 15 | v.push_back(v1); 16 | v.push_back(v2); 17 | v.push_back(v3); 18 | v.push_back(v4); 19 | 20 | for (int i = 0 ; i < v.size() ; i++) { 21 | for (int j = 0; j < v[i].size(); ++j) 22 | { 23 | cout << v[i][j] << " "; 24 | } 25 | cout << endl; 26 | } 27 | 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Lecture-21/ElephantWays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-21/ElephantWays -------------------------------------------------------------------------------- /Lecture-21/ElephantWays.cpp: -------------------------------------------------------------------------------- 1 | // ElephantWays.cpp 2 | #include 3 | using namespace std; 4 | 5 | int ElephantWays(int i, int j) { 6 | // base case 7 | if (i == 0 and j == 0) { 8 | return 1; 9 | } 10 | 11 | int ans = 0; 12 | for (int k = 0; k < i; ++k) 13 | { 14 | ans += ElephantWays(k, j); 15 | } 16 | 17 | for (int k = 0; k < j; ++k) 18 | { 19 | ans += ElephantWays(i, k); 20 | } 21 | return ans; 22 | } 23 | 24 | int main() { 25 | 26 | int n, m; 27 | cin >> n >> m; 28 | 29 | cout << ElephantWays(n, m) << endl; 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lecture-21/Jayani: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-21/Jayani -------------------------------------------------------------------------------- /Lecture-21/Jayani.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findDigitSum(int x) { 5 | int ans = 0; 6 | while (x > 0) { 7 | ans += x % 10; 8 | x /= 10; 9 | } 10 | return ans; 11 | } 12 | 13 | int main() { 14 | 15 | int n = 30; 16 | 17 | // cin >> n; 18 | int cnt = 0; 19 | 20 | for (int i = 2; i <= n; i ++) 21 | { 22 | int digit_sum = findDigitSum(i); 23 | if (digit_sum % 2 == 0) { 24 | cnt++; 25 | } 26 | } 27 | 28 | cout << cnt << endl; 29 | 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lecture-21/PhoneKeypad: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-21/PhoneKeypad -------------------------------------------------------------------------------- /Lecture-21/PhoneKeypad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | char keys[][10] = { 6 | "", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ" 7 | }; 8 | 9 | string contacts[100] = { 10 | "VARUN", 11 | "NOBITA", 12 | "GIAN", 13 | "DORAEMON", 14 | "HIMANI" 15 | }; 16 | 17 | void PhoneKeypad(char*in, int i, char *out, int j) { 18 | // base case 19 | if (in[i] == '\0') { // 82786: Varun aaega ispar 20 | out[j] = '\0'; 21 | string x(out); // x ek string bann gai jiski value is same as out 22 | for (int i = 0 ; i < 5 ; i++) { 23 | if (x == contacts[i]) 24 | cout << x << endl; 25 | } 26 | return; 27 | } 28 | // recursive case 29 | int d = in[i] - '0'; 30 | for (int k = 0; keys[d][k] != '\0'; ++k) 31 | { 32 | out[j] = keys[d][k]; 33 | PhoneKeypad(in, i + 1, out, j + 1); 34 | } 35 | } 36 | 37 | int main() { 38 | 39 | char in[100], out[100]; 40 | 41 | cin >> in; 42 | PhoneKeypad(in, 0, out, 0); 43 | 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /Lecture-21/Vector-Array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-21/Vector-Array -------------------------------------------------------------------------------- /Lecture-21/Vector-Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // vector v(number_of_buckets,initial_value); // Capacity: 10000 8 | // vector v(10000); // Capacity: 10000 9 | 10 | int n = 10; 11 | // cin >> n; 12 | vector v(n, 10); 13 | cout << v.capacity() << endl; 14 | 15 | for (int i = 0; i < n; ++i) 16 | { 17 | v[i] = i + 1; 18 | } 19 | 20 | reverse(v.begin(), v.end()); 21 | 22 | for (int i = 0; i < n; ++i) 23 | { 24 | cout << v[i] << " "; 25 | } 26 | cout << endl; 27 | 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Lecture-21/Vectors: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-21/Vectors -------------------------------------------------------------------------------- /Lecture-21/Vectors.cpp: -------------------------------------------------------------------------------- 1 | // Vectors.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | // vector name; 8 | vector v; 9 | 10 | for (int i = 1; i <= 5; ++i) 11 | { 12 | cout << "Inserting: " << i << " : "; 13 | v.push_back(i); 14 | cout << " Size: " << v.size() << ", Capacity: " << v.capacity() << endl; 15 | } 16 | 17 | for (int i = 0; i < v.size(); ++i) 18 | { 19 | cout << v[i] << " "; 20 | } 21 | cout << endl; 22 | 23 | // VECTOR INITIALIZATION 24 | // 1st way 25 | // vector v1({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); 26 | 27 | // 2nd way 28 | // vector v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 29 | 30 | // 3rd way 31 | int a[] = {1, 2, 3, 4, 5, 6}; 32 | int n = sizeof(a) / sizeof(int); 33 | vector v1(a, a + n); 34 | 35 | for (int i = 0; i < v1.size(); ++i) 36 | { 37 | cout << v1[i] << " "; 38 | } 39 | cout << endl; 40 | 41 | 42 | return 0; 43 | } 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Lecture-22/CombinationSum.cpp: -------------------------------------------------------------------------------- 1 | // CombinationSum.cpp 2 | // https://leetcode.com/problems/combination-sum/ 3 | class Solution { 4 | public: 5 | vector > ans; 6 | void solve(vector& candidates, int target, int s, vector &x) { 7 | // base case 8 | if (target == 0) { 9 | ans.push_back(x); 10 | return; 11 | } 12 | 13 | // recursive case 14 | for (int i = s ; i < candidates.size() ; i ++) { 15 | if (target >= candidates[i]) { 16 | x.push_back(candidates[i]); 17 | solve(candidates, target - candidates[i], i, x); 18 | x.pop_back(); 19 | } 20 | } 21 | } 22 | 23 | vector> combinationSum(vector& candidates, int target) { 24 | vector x; 25 | solve(candidates, target, 0, x); 26 | return ans; 27 | } 28 | }; -------------------------------------------------------------------------------- /Lecture-22/CombinationSum2.: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> combinationSum2(vector& candidates, int target) { 4 | 5 | } 6 | }; -------------------------------------------------------------------------------- /Lecture-22/CombinationSum2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | void solve(vector &candidates, int target, int s, vector &x) { 5 | // base case 6 | if (target == 0) { 7 | ans.push_back(x); 8 | return; 9 | } 10 | if (target < 0) { 11 | return; 12 | } 13 | // recursive case 14 | for (int i = s; i < candidates.size(); ++i) 15 | { 16 | if (i == s || candidates[i] != candidates[i - 1]) { 17 | x.push_back(candidates[i]); 18 | solve(candidates, target - candidates[i], i + 1, x); 19 | x.pop_back(); 20 | } 21 | } 22 | } 23 | 24 | vector> combinationSum2(vector& candidates, int target) { 25 | vector x; 26 | sort(candidates.begin(), candidates.end()); 27 | solve(candidates, target, 0, x); 28 | 29 | return ans; 30 | } 31 | }; -------------------------------------------------------------------------------- /Lecture-22/Combinations: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-22/Combinations -------------------------------------------------------------------------------- /Lecture-22/Combinations.cpp: -------------------------------------------------------------------------------- 1 | // Combinations.cpp 2 | #include 3 | using namespace std; 4 | 5 | int kd; // is always equals to k 6 | void solve(int n, int k, int *out, int j, int s) { 7 | // base case 8 | if (k == 0) { 9 | // print the output array 10 | for (int i = 0; i < kd; ++i) 11 | { 12 | cout << out[i] << " "; 13 | } 14 | cout << endl; 15 | return; 16 | } 17 | // recursive case 18 | for (int i = s; i <= n; ++i) 19 | { 20 | out[j] = i; 21 | solve(n, k - 1, out, j + 1, i + 1); 22 | } 23 | } 24 | 25 | int main() { 26 | 27 | int n = 4; 28 | int k = 3; 29 | int out[100]; 30 | 31 | kd = k; 32 | solve(n, k, out, 0, 1); 33 | 34 | 35 | return 0; 36 | } 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Lecture-22/Test.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | 5 | void solve(int s, vector &v, int k, int n) { 6 | // base case 7 | if (k == 0) { 8 | ans.push_back(v); 9 | return; 10 | } 11 | // recursive case 12 | for (int i = s; i <= n; ++i) 13 | { 14 | v.push_back(i); 15 | solve(i + 1, v, k - 1, n); 16 | v.pop_back(); 17 | } 18 | } 19 | 20 | vector> combine(int n, int k) { 21 | vector v; 22 | solve(1, v, k, n); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Lecture-23/DMA2D: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-23/DMA2D -------------------------------------------------------------------------------- /Lecture-23/DMA2D.cpp: -------------------------------------------------------------------------------- 1 | // DMA2D.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int rows, cols; 7 | cin >> rows >> cols; 8 | 9 | int **a = new int*[rows]; 10 | 11 | for (int i = 0; i < rows; ++i) 12 | { 13 | a[i] = new int[cols]; 14 | } 15 | 16 | int number = 1; 17 | for (int i = 0; i < rows; ++i) 18 | { 19 | for (int j = 0; j < cols; ++j) 20 | { 21 | a[i][j] = number ++; 22 | } 23 | } 24 | 25 | for (int i = 0; i < rows; ++i) 26 | { 27 | for (int j = 0; j < cols; ++j) 28 | { 29 | cout << a[i][j] << " "; 30 | } 31 | cout << endl; 32 | } 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Lecture-23/OOPS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-23/OOPS -------------------------------------------------------------------------------- /Lecture-23/OOPS.cpp: -------------------------------------------------------------------------------- 1 | // OOPS.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //////////////////////////////////// BLUEPRINT /////////////////////////////////// 7 | class Car { 8 | public: 9 | // Data Members 10 | char name[100]; 11 | int model; 12 | int price; 13 | int seats; 14 | 15 | ///////////////////////// 1. DEFAULT CONSTRUCTOR /////////////////////////////// 16 | Car() { 17 | cout << "Inside Default constructor\n"; 18 | } 19 | ///////////////////////// 1.!DEFAULT CONSTRUCTOR /////////////////////////////// 20 | 21 | // Functions 22 | void print() { 23 | cout << "Name : " << name << endl; 24 | cout << "Price : " << price << endl; 25 | cout << "Model : " << model << endl; 26 | cout << "Seats : " << seats << endl << endl; 27 | } 28 | }; 29 | /////////////////////////////////// !BLUEPRINT /////////////////////////////////// 30 | 31 | int main() { 32 | 33 | Car A; // A is an object of class Car 34 | // A.name = "BMW"; 35 | // A.name[0] = 'B'; 36 | // A.name[1] = 'M'; 37 | // A.name[2] = 'W'; 38 | // A.name[3] = '\0'; 39 | strcpy(A.name, "BMW"); 40 | 41 | A.price = 1000; 42 | A.seats = 4; 43 | A.model = 2018; 44 | // cout << A.name << endl; 45 | // cout << A.price << endl; 46 | // cout << A.model << endl; 47 | // cout << A.seats << endl; 48 | 49 | Car B; 50 | strcpy(B.name, "Audi"); 51 | B.price = 500; 52 | B.seats = 2; 53 | B.model = 2020; 54 | 55 | 56 | A.print(); 57 | B.print(); 58 | 59 | 60 | 61 | 62 | 63 | return 0; 64 | } 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Lecture-23/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-23/Test -------------------------------------------------------------------------------- /Lecture-23/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int *createArray(int n) { 5 | int *a = new int[n]; 6 | for (int i = 0; i < n; ++i) 7 | { 8 | a[i] = i * i; 9 | } 10 | return a; 11 | } 12 | 13 | int main() { 14 | 15 | int *a = new int; 16 | 17 | *a = 10; 18 | cout << *a << endl; 19 | 20 | float *f = new float; 21 | *f = 11.11; 22 | cout << *f << endl; 23 | 24 | //////////////////////// CREATE ARRAY //////////////////////// 25 | /* 26 | int n; 27 | cin >> n; 28 | int *arr = new int[n]; 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | arr[i] = i + 1; 33 | } 34 | 35 | for (int i = 0; i < n; ++i) 36 | { 37 | cout << arr[i] << " "; 38 | } 39 | cout << endl; 40 | */ 41 | 42 | int *arr = createArray(10); 43 | for (int i = 0; i < 10; ++i) 44 | { 45 | cout << arr[i] << " "; 46 | } 47 | cout << endl; 48 | 49 | int *x = new int; 50 | *x = 100; 51 | 52 | cout << *x + 20 << endl; 53 | 54 | delete x; 55 | x = NULL; 56 | 57 | if (*x > 0) { 58 | cout << "Take right\n"; 59 | } 60 | else { 61 | cout << "Take left\n"; 62 | } 63 | 64 | cout << *x << endl; 65 | 66 | return 0; 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /Lecture-24/OOPS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-24/OOPS -------------------------------------------------------------------------------- /Lecture-25/OOPs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-25/OOPs -------------------------------------------------------------------------------- /Lecture-25/Vectors: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-25/Vectors -------------------------------------------------------------------------------- /Lecture-25/Vectors.cpp: -------------------------------------------------------------------------------- 1 | // Vectors.cpp 2 | #include 3 | using namespace std; 4 | 5 | class Vector { 6 | public: 7 | int cs; 8 | int cap; 9 | int *a; 10 | 11 | Vector(int s = 1) { 12 | a = new int [s]; 13 | cs = 0; 14 | cap = s; 15 | } 16 | 17 | void push_back(int d) { 18 | if (cs == cap) { 19 | int *olda = a; 20 | int oldcap = cap; 21 | 22 | a = new int[2 * cap]; 23 | cap *= 2; 24 | for (int i = 0; i < oldcap; ++i) 25 | { 26 | a[i] = olda[i]; 27 | } 28 | delete[] olda; 29 | } 30 | a[cs++] = d; 31 | } 32 | 33 | void pop_back() { 34 | if (cs > 0) { 35 | cs--; 36 | } 37 | } 38 | 39 | bool empty() { 40 | return cs == 0; 41 | } 42 | 43 | int size() { 44 | return cs; 45 | } 46 | 47 | int capacity() { 48 | return cap; 49 | } 50 | 51 | int operator[](int i) { 52 | return a[i]; 53 | } 54 | }; 55 | 56 | int main() { 57 | 58 | Vector v; 59 | 60 | v.push_back(1); 61 | v.push_back(2); 62 | v.push_back(3); 63 | v.push_back(4); 64 | v.push_back(5); 65 | 66 | cout << "Capcity: " << v.capacity() << endl; 67 | cout << "Size: " << v.size() << endl; 68 | 69 | for (int i = 0; i < v.size(); ++i) 70 | { 71 | cout << v[i] << " "; 72 | } 73 | cout << endl; 74 | 75 | return 0; 76 | } 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /Lecture-26/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-26/Test -------------------------------------------------------------------------------- /Lecture-26/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isPrime(int n) { 6 | // BigO(sqrt(N)) 7 | for (int i = 2; i * i <= n; ++i) 8 | { 9 | if (n % i == 0) { 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | bool isPrime1(int n) { 17 | if (n != 2 and n % 2 == 0) return false; 18 | // BigO(sqrt(N)/2) 19 | for (int i = 3; i * i <= n; i += 2) 20 | { 21 | if (n % i == 0) { 22 | return false; 23 | } 24 | } 25 | return true; 26 | } 27 | 28 | void primeSieve(int n) { 29 | bool *a = new bool[n + 1]; 30 | for (int i = 0; i <= n; ++i) { 31 | a[i] = true; 32 | } 33 | 34 | for (int i = 2; i <= n; ++i) { 35 | if (a[i] == true) { 36 | for (int j = 2 * i; j <= n; j += i) 37 | { 38 | a[j] = false; 39 | } 40 | 41 | } 42 | } 43 | for (int i = 2; i <= n; ++i) { 44 | if (a[i] == true) { 45 | cout << i << " "; 46 | } 47 | } 48 | cout << endl; 49 | } 50 | 51 | int main() { 52 | 53 | int n = 100; 54 | // cin >> n; 55 | for (int i = 2; i <= n; ++i) 56 | { 57 | if (isPrime1(i)) { 58 | cout << i << " "; 59 | } 60 | } 61 | 62 | cout << endl; 63 | 64 | primeSieve(n); 65 | 66 | 67 | return 0; 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /Lecture-27/LinkedList: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-27/LinkedList -------------------------------------------------------------------------------- /Lecture-27/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void print(int n) { 5 | if (n == 0) return; 6 | 7 | cout << n << " "; 8 | print(n - 2); 9 | } 10 | 11 | void print1(int n) { 12 | if (n == 0) return; 13 | 14 | cout << n << " "; 15 | print(n - 2); 16 | } 17 | 18 | int main() { 19 | 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-28/Subsets-2.cpp: -------------------------------------------------------------------------------- 1 | // Subsets.cpp 2 | class Solution { 3 | public: 4 | vector> ans; 5 | void solve(vector &nums, int i, vector &x) { 6 | ans.push_back(x); 7 | if (i == nums.size()) { 8 | return; 9 | } 10 | 11 | for (int j = i; j < nums.size(); ++j) 12 | { 13 | if (i != j and nums[j] == nums[j - 1]) { 14 | continue; 15 | } 16 | else { 17 | x.push_back(nums[j]); 18 | solve(nums, j + 1, x); 19 | x.pop_back(); 20 | } 21 | } 22 | } 23 | 24 | vector> subsetsWithDup(vector& nums) { 25 | sort(nums.begin(), nums.end()); 26 | vector x; 27 | solve(nums, 0, x); 28 | return ans; 29 | } 30 | }; -------------------------------------------------------------------------------- /Lecture-28/Subsets.cpp: -------------------------------------------------------------------------------- 1 | // Subsets.cpp 2 | class Solution { 3 | public: 4 | vector> ans; 5 | void solve(vector &nums, int i, vector &x) { 6 | // base case 7 | if (i == nums.size()) { 8 | ans.push_back(x); 9 | return; 10 | } 11 | // recursive case 12 | // pehla case include mat karo inside x 13 | solve(nums, i + 1, x); 14 | // doosra case, include karlo inside x 15 | x.push_back(nums[i]); 16 | solve(nums, i + 1, x); 17 | x.pop_back(); 18 | } 19 | 20 | vector> subsets(vector& nums) { 21 | vector x; 22 | solve(nums, 0, x); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Lecture-29/LinkedList: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-29/LinkedList -------------------------------------------------------------------------------- /Lecture-3/AsciiCodes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-3/AsciiCodes -------------------------------------------------------------------------------- /Lecture-3/AsciiCodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char ch; 6 | int i = 97; 7 | 8 | while (i <= 122) { 9 | ch = i; 10 | 11 | cout << ch << " " << i << endl; 12 | 13 | i = i + 1; 14 | } 15 | 16 | return 0; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lecture-3/CelAndFah: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-3/CelAndFah -------------------------------------------------------------------------------- /Lecture-3/CelAndFah.cpp: -------------------------------------------------------------------------------- 1 | // CelAndFah.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int init, fval, step, f, c; 8 | cin >> init >> fval >> step; 9 | 10 | f = init; 11 | while (f <= fval) { 12 | // c = (5 / 9.0) * (f - 32); 13 | c = 5 * (f - 32) / 9; 14 | 15 | cout << f << " " << c << endl; 16 | 17 | 18 | f = f + step; 19 | } 20 | 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Lecture-3/SquareRoot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-3/SquareRoot -------------------------------------------------------------------------------- /Lecture-3/SquareRoot.cpp: -------------------------------------------------------------------------------- 1 | // SquareRoot.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | double i = 0, inc = 1; 11 | int x = 1; 12 | while (x <= 4) { 13 | while (i * i <= n) { 14 | i = i + inc; 15 | } 16 | 17 | i = i - inc; 18 | 19 | inc = inc / 10; 20 | x = x + 1; 21 | } 22 | cout << i << endl; 23 | 24 | return 0; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Lecture-3/StarPattern: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-3/StarPattern -------------------------------------------------------------------------------- /Lecture-3/StarPattern.cpp: -------------------------------------------------------------------------------- 1 | // StarPattern.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, row, stars, number; 7 | cin >> n; 8 | 9 | row = 1, number = 1; 10 | while (row <= n) { 11 | // Print stars row times 12 | stars = 1; 13 | while (stars <= row) { 14 | cout << number << " "; 15 | number = number + 1; 16 | stars = stars + 1; 17 | } 18 | 19 | cout << '\n'; 20 | row = row + 1; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-3/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Lecture-3/UpperAndLower: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-3/UpperAndLower -------------------------------------------------------------------------------- /Lecture-3/UpperAndLower.cpp: -------------------------------------------------------------------------------- 1 | // UpperAndLower.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | char ch; 8 | cin >> ch; 9 | 10 | int x = ch; 11 | 12 | if (x >= 65 and x <= 90) { 13 | cout << "Upper Case" << endl; 14 | } 15 | else { 16 | cout << "Lower Case" << endl; 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lecture-3/UpperAndLowercase-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-3/UpperAndLowercase-1 -------------------------------------------------------------------------------- /Lecture-3/UpperAndLowercase-1.cpp: -------------------------------------------------------------------------------- 1 | // UpperAndLowercase - 1.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | char ch; 8 | cin >> ch; 9 | 10 | if (ch >= 'A' and ch <= 'Z') { 11 | cout << "Upper case" << endl; 12 | } 13 | else { 14 | cout << "Lower case" << endl; 15 | } 16 | 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lecture-30/BalancedParanthesis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-30/BalancedParanthesis -------------------------------------------------------------------------------- /Lecture-30/BalancedParanthesis.cpp: -------------------------------------------------------------------------------- 1 | // BalancedParanthesis.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool isBalancedParenthesis(char *a) { 7 | stack s; 8 | 9 | for (int i = 0; a[i] != '\0'; ++i) 10 | { 11 | char ch = a[i]; 12 | switch (ch) { 13 | case '{': 14 | case '(': 15 | case '[': 16 | s.push(ch); 17 | break; 18 | case ')': 19 | if (s.empty() || s.top() != '(') { 20 | return false; 21 | } 22 | s.pop(); 23 | break; 24 | case '}': 25 | if (s.empty() || s.top() != '{') { 26 | return false; 27 | } 28 | s.pop(); 29 | break; 30 | case ']': 31 | if (s.empty() || s.top() != '[') { 32 | return false; 33 | } 34 | s.pop(); 35 | } 36 | } 37 | 38 | if (s.empty()) return true; 39 | else return false; 40 | } 41 | 42 | int main() { 43 | 44 | char a[] = "{a+[b*(c+d)]*e}"; 45 | 46 | if (isBalancedParenthesis(a)) { 47 | cout << "Balanced hai\n"; 48 | } 49 | else { 50 | cout << "Balanced nahi hai\n"; 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /Lecture-30/Queue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-30/Queue -------------------------------------------------------------------------------- /Lecture-30/Queue-LL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-30/Queue-LL -------------------------------------------------------------------------------- /Lecture-30/Queue-LL.cpp: -------------------------------------------------------------------------------- 1 | // Queue - LL.cpp 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* next; 9 | node(int d) { 10 | data = d; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | class Queue { 16 | node* head, *tail; 17 | int cs; 18 | public: 19 | Queue() { 20 | head = tail = NULL; 21 | cs = 0; 22 | } 23 | 24 | void push(int d) { 25 | if (!head) { 26 | head = tail = new node(d); 27 | } 28 | else { 29 | node*n = new node(d); 30 | tail->next = n; 31 | tail = n; 32 | } 33 | cs++; 34 | } 35 | 36 | void pop() { 37 | if (!head)return; 38 | else if (head->next == NULL) { 39 | delete head; 40 | head = tail = NULL; 41 | cs--; 42 | } 43 | else { 44 | node* temp = head; 45 | head = head->next; 46 | delete temp; 47 | cs--; 48 | } 49 | } 50 | 51 | bool empty() { 52 | return head == NULL; 53 | } 54 | 55 | int front() { 56 | return head->data; 57 | } 58 | 59 | int size() { 60 | return cs; 61 | } 62 | }; 63 | 64 | int main() { 65 | Queue q; 66 | q.push(1); 67 | q.push(2); 68 | q.push(3); 69 | q.push(4); 70 | 71 | while (!q.empty()) { 72 | cout << q.front() << " "; 73 | q.pop(); 74 | } 75 | 76 | 77 | return 0; 78 | } 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /Lecture-30/Queue.cpp: -------------------------------------------------------------------------------- 1 | // Queue.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | queue q; 9 | q.push(1); 10 | q.push(2); 11 | q.push(3); 12 | q.push(4); 13 | 14 | while (!q.empty()) { 15 | cout << q.front() << " "; 16 | q.pop(); 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lecture-30/ReverseStack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-30/ReverseStack -------------------------------------------------------------------------------- /Lecture-30/ReverseStack.cpp: -------------------------------------------------------------------------------- 1 | // ReverseStack.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void push_bottom(stack &s, 7 | int topElement) { 8 | // base case 9 | if (s.empty()) { 10 | s.push(topElement); 11 | return; 12 | } 13 | // recursive case 14 | int top = s.top(); 15 | s.pop(); 16 | push_bottom(s, topElement); 17 | s.push(top); 18 | } 19 | 20 | void ReverseStack(stack &s) { 21 | if (s.empty()) { 22 | return; 23 | } 24 | 25 | int topElement = s.top(); 26 | s.pop(); 27 | ReverseStack(s); 28 | push_bottom(s, topElement); 29 | } 30 | 31 | 32 | void printStack(stack s) { 33 | 34 | while (!s.empty()) { 35 | cout << s.top() << " "; 36 | s.pop(); 37 | } 38 | 39 | cout << endl; 40 | } 41 | 42 | int main() { 43 | 44 | stack s; 45 | 46 | s.push(1); 47 | s.push(2); 48 | s.push(3); 49 | s.push(4); 50 | 51 | printStack(s); 52 | ReverseStack(s); 53 | printStack(s); 54 | 55 | 56 | return 0; 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /Lecture-30/Stack-LL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-30/Stack-LL -------------------------------------------------------------------------------- /Lecture-30/Stack-LL.cpp: -------------------------------------------------------------------------------- 1 | // Stack - LL.cpp 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* next; 9 | node(int d) { 10 | data = d; 11 | next = NULL; 12 | } 13 | }; 14 | 15 | class Stack { 16 | node* head; 17 | public: 18 | Stack() { 19 | head = NULL; 20 | } 21 | 22 | void push(int d) { 23 | if (head == NULL) { 24 | head = new node(d); 25 | } 26 | else { 27 | node* n = new node(d); 28 | n->next = head; 29 | head = n; 30 | } 31 | } 32 | 33 | void pop() { 34 | if (!head) return; 35 | 36 | node* temp = head; 37 | head = head->next; 38 | delete temp; 39 | } 40 | 41 | 42 | bool empty() { 43 | return head == NULL; 44 | } 45 | 46 | int top() { 47 | return head->data; 48 | } 49 | }; 50 | 51 | int main() { 52 | 53 | Stack s; 54 | s.push(1); 55 | s.push(2); 56 | s.push(3); 57 | s.push(4); 58 | 59 | while (!s.empty()) { 60 | cout << s.top() << " "; 61 | s.pop(); 62 | } 63 | 64 | cout << endl; 65 | 66 | return 0; 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /Lecture-30/Stack-Vector: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-30/Stack-Vector -------------------------------------------------------------------------------- /Lecture-30/Stack-Vector.cpp: -------------------------------------------------------------------------------- 1 | // Stack - Vector.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Stack { 7 | vector v; 8 | public: 9 | 10 | void push(int d) { 11 | v.push_back(d); 12 | } 13 | 14 | void pop() { 15 | v.pop_back(); 16 | } 17 | 18 | bool empty() { 19 | return v.size() == 0; 20 | } 21 | 22 | int top() { 23 | return v[v.size() - 1]; 24 | } 25 | }; 26 | 27 | int main() { 28 | 29 | Stack s; 30 | s.push(1); 31 | s.push(2); 32 | s.push(3); 33 | s.push(4); 34 | 35 | while (!s.empty()) { 36 | cout << s.top() << " "; 37 | s.pop(); 38 | } 39 | 40 | cout << endl; 41 | 42 | return 0; 43 | } 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Lecture-30/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-30/Test -------------------------------------------------------------------------------- /Lecture-30/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | stack s; 8 | s.push(1); 9 | s.push(2); 10 | s.push(3); 11 | s.push(4); 12 | 13 | while (!s.empty()) { 14 | cout << s.top() << " "; 15 | s.pop(); 16 | } 17 | 18 | cout << endl; 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Lecture-31(QPS)/FirstMissing.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& a) { 4 | int n = a.size(); 5 | 6 | for (int i = 0; i < n; ++i) 7 | { 8 | while (a[i] > 0 and a[i] <= n and a[a[i] - 1] != a[i]) { 9 | swap(a[i], a[a[i] - 1]); 10 | } 11 | } 12 | 13 | for (int i = 0; i < n; ++i) 14 | { 15 | if (a[i] != i + 1) return i + 1; 16 | } 17 | return n + 1; 18 | } 19 | }; -------------------------------------------------------------------------------- /Lecture-31(QPS)/PalindromePartition.cpp: -------------------------------------------------------------------------------- 1 | #define vvs vector> 2 | class Solution { 3 | public: 4 | // Car A = B; 5 | // string x = s;// Copy constructor 6 | bool isPalindrome(string &x) { 7 | int i = 0; 8 | int j = x.length() - 1; 9 | while (i < j) { 10 | if (x[i++] != x[j--]) return false; 11 | } 12 | return true; 13 | } 14 | vvs ans; 15 | void solve(string &s, int i, vector &temp) { 16 | if (i == s.length()) { 17 | ans.push_back(temp); 18 | return; 19 | } 20 | 21 | for (int j = i; j < s.length(); ++j) 22 | { 23 | string x = s.substr(i, j - i + 1); 24 | if (isPalindrome(x)) { 25 | temp.push_back(x); 26 | solve(s, j + 1, temp); 27 | temp.pop_back(); 28 | } 29 | } 30 | } 31 | 32 | 33 | vvs partition(string &s) { 34 | vector temp; 35 | solve(s, 0, temp); 36 | return ans; 37 | } 38 | }; 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Lecture-31(QPS)/Permutation.cpp: -------------------------------------------------------------------------------- 1 | #define vvi vector> 2 | #define vi vector 3 | class Solution { 4 | public: 5 | vvi ans; 6 | 7 | void solve(vi& nums, int i) { 8 | if (i == nums.size()) { 9 | ans.push_back(nums); 10 | return; 11 | } 12 | 13 | for (int j = i; j < nums.size(); ++j) 14 | { 15 | swap(nums[i], nums[j]); 16 | solve(nums, i + 1); 17 | swap(nums[i], nums[j]); 18 | } 19 | } 20 | 21 | vvi permute(vi& nums) { 22 | solve(nums, 0); 23 | 24 | return ans; 25 | } 26 | }; -------------------------------------------------------------------------------- /Lecture-31(QPS)/wordsearch.cpp: -------------------------------------------------------------------------------- 1 | #define vvc vector> 2 | class Solution { 3 | public: 4 | bool kyaWordMilla(vvc& a, string &word, 5 | int i, int j, int k, int n, int m) { 6 | // base case 7 | if (k == word.size()) return true; 8 | // recursive case 9 | if (i < 0 || j < 0 || i >= n || j >= m || 10 | word[k] != a[i][j]) return false; 11 | 12 | char ch = a[i][j]; 13 | a[i][j] = '.'; 14 | int x[] = {1, 0, -1, 0}; 15 | int y[] = {0, 1, 0, -1}; 16 | 17 | for (int l = 0; l < 4; ++l) 18 | { 19 | int ni = i + x[l]; 20 | int nj = j + y[l]; 21 | bool kyaKaamHua = kyaWordMilla(a, word, ni, nj, k + 1, n, m); 22 | if (kyaKaamHua) return true; 23 | } 24 | a[i][j] = ch; 25 | return false; 26 | } 27 | 28 | bool exist(vvc& board, string &word) { 29 | int n = board.size(); 30 | int m = board[0].size(); 31 | for (int i = 0; i < n; ++i) 32 | { 33 | for (int j = 0; j < m; ++j) 34 | { 35 | if (kyaWordMilla(board, word, i, j, 0, n, m)) { 36 | return true; 37 | } 38 | } 39 | } 40 | return false; 41 | } 42 | }; 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Lecture-32/BinaryTrees: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-32/BinaryTrees -------------------------------------------------------------------------------- /Lecture-32/BinaryTrees.cpp: -------------------------------------------------------------------------------- 1 | // BinaryTrees.cpp 2 | #include 3 | using namespace std; 4 | // 8 10 1 -1 -1 6 4 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1 5 | class node 6 | { 7 | public: 8 | int data; 9 | node* left; 10 | node* right; 11 | node(int d) { 12 | data = d; 13 | left = right = NULL; 14 | } 15 | }; 16 | 17 | node* createTree() { 18 | // base case 19 | int data; 20 | cin >> data; 21 | if (data == -1) { 22 | return NULL; 23 | } 24 | // recursive case 25 | node* root = new node(data); 26 | root->left = createTree(); 27 | root->right = createTree(); 28 | return root; 29 | } 30 | 31 | void preorder(node* root) { 32 | if (root == NULL) { 33 | return; 34 | } 35 | cout << root->data << " "; 36 | preorder(root->left); 37 | preorder(root->right); 38 | } 39 | 40 | void inorder(node* root) { 41 | if (root == NULL) { 42 | return; 43 | } 44 | inorder(root->left); 45 | cout << root->data << " "; 46 | inorder(root->right); 47 | } 48 | 49 | void postorder(node* root) { 50 | if (root == NULL) { 51 | return; 52 | } 53 | postorder(root->left); 54 | postorder(root->right); 55 | cout << root->data << " "; 56 | } 57 | 58 | int countNodes(node* root) { 59 | // base case 60 | if (!root) return 0; 61 | 62 | // recursive case 63 | int leftKaCount = countNodes(root->left); 64 | int rightKaCount = countNodes(root->right); 65 | return leftKaCount + rightKaCount + 1; 66 | } 67 | 68 | int main() { 69 | 70 | node *root = createTree(); 71 | 72 | preorder(root); 73 | cout << endl; 74 | inorder(root); 75 | cout << endl; 76 | postorder(root); 77 | cout << endl; 78 | 79 | cout << "Total Nodes: " << countNodes(root) << endl; 80 | 81 | return 0; 82 | } 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /Lecture-32/CircularQueue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-32/CircularQueue -------------------------------------------------------------------------------- /Lecture-32/CircularQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue { 5 | int *a, n, cs, f, e; 6 | public: 7 | Queue(int s = 5) { 8 | a = new int[s]; 9 | n = s; 10 | cs = 0; 11 | f = 0; 12 | e = n - 1; 13 | } 14 | 15 | void push(int d) { 16 | if (cs < n) { 17 | e = (e + 1) % n; 18 | a[e] = d; 19 | cs++; 20 | } 21 | else { 22 | cout << "Over flow\n"; 23 | } 24 | } 25 | 26 | void pop() { 27 | if (cs > 0) { 28 | f = (f + 1) % n; 29 | cs--; 30 | } 31 | else { 32 | cout << "Under flow\n"; 33 | } 34 | } 35 | 36 | bool empty() { 37 | return cs == 0; 38 | } 39 | 40 | int size() { 41 | return cs; 42 | } 43 | 44 | int front() { 45 | return a[f]; 46 | } 47 | 48 | }; 49 | 50 | int main() { 51 | 52 | Queue q(6); 53 | q.push(1); 54 | q.push(2); 55 | q.push(3); 56 | q.push(4); 57 | q.push(5); 58 | q.push(6); 59 | 60 | while (q.empty() != true) { 61 | cout << q.front() << " "; 62 | q.pop(); 63 | } 64 | 65 | cout << endl; 66 | 67 | return 0; 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /Lecture-32/StackUsingQueue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-32/StackUsingQueue -------------------------------------------------------------------------------- /Lecture-32/StackUsingQueue.cpp: -------------------------------------------------------------------------------- 1 | // StackUsingQueue.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Stack { 7 | queue q1; 8 | queue q2; 9 | public: 10 | void push(int d) { 11 | if (q1.empty() and q2.empty()) { 12 | q1.push(d); 13 | } 14 | else if (!q1.empty() and q2.empty()) { 15 | q1.push(d); 16 | } 17 | else { 18 | q2.push(d); 19 | } 20 | } 21 | 22 | void pop() { 23 | if (!q1.empty() and q2.empty()) { 24 | while (q1.size() > 1) { 25 | int f = q1.front(); 26 | q1.pop(); 27 | q2.push(f); 28 | } 29 | q1.pop(); 30 | } 31 | else { 32 | while (q2.size() > 1) { 33 | int f = q2.front(); 34 | q2.pop(); 35 | q1.push(f); 36 | } 37 | q2.pop(); 38 | } 39 | } 40 | 41 | bool empty() { 42 | if (q1.empty() and q2.empty()) { 43 | return true; 44 | } 45 | return false; 46 | } 47 | 48 | int top() { 49 | if (!q1.empty() and q2.empty()) { 50 | while (q1.size() > 1) { 51 | int f = q1.front(); 52 | q1.pop(); 53 | q2.push(f); 54 | } 55 | 56 | int temp = q1.front(); 57 | q1.pop(); 58 | q2.push(temp); 59 | return temp; 60 | } 61 | else { 62 | while (q2.size() > 1) { 63 | int f = q2.front(); 64 | q2.pop(); 65 | q1.push(f); 66 | } 67 | 68 | int temp = q2.front(); 69 | q2.pop(); 70 | q1.push(temp); 71 | return temp; 72 | } 73 | } 74 | }; 75 | 76 | int main() { 77 | 78 | Stack s; 79 | s.push(1); 80 | s.push(2); 81 | s.push(3); 82 | s.push(4); 83 | 84 | while (!s.empty()) { 85 | cout << s.top() << endl; 86 | s.pop(); 87 | } 88 | 89 | 90 | return 0; 91 | } 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /Lecture-33/BinaryTrees: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-33/BinaryTrees -------------------------------------------------------------------------------- /Lecture-33/input.txt: -------------------------------------------------------------------------------- 1 | 8 10 1 -1 -1 6 4 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1 -------------------------------------------------------------------------------- /Lecture-34(QPS)/Subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> x; 4 | 5 | void solve(vector& a, int i, vector &ans) { 6 | x.push_back(ans); 7 | 8 | if (i == a.size()) return; 9 | 10 | 11 | for (int j = i; j < a.size(); ++j) 12 | { 13 | if (j == i || a[j] != a[j - 1]) { 14 | ans.push_back(a[j]); 15 | solve(a, j + 1, ans); 16 | ans.pop_back(); 17 | } 18 | } 19 | } 20 | 21 | vector> subsetsWithDup(vector& nums) { 22 | vector ans; 23 | sort(nums.begin(), nums.end()); 24 | solve(nums, 0, ans); 25 | return x; 26 | } 27 | }; -------------------------------------------------------------------------------- /Lecture-34(QPS)/Test.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(int *freq) { 4 | int ans = 0; 5 | for (int i = 0; i < 26; ++i) 6 | { 7 | if (freq[i] > 0) { 8 | freq[i] --; 9 | ans += solve(freq) + 1; 10 | freq[i]++; 11 | } 12 | } 13 | return ans; 14 | } 15 | 16 | int numTilePossibilities(string a) { 17 | int freq[26] = {0}; 18 | // prepare the freq array 19 | for (int i = 0; i < a.length(); ++i) 20 | { 21 | char ch = a[i]; 22 | int indx = ch - 'A'; 23 | freq[indx]++; 24 | } 25 | 26 | int ans = solve(freq); 27 | return ans; 28 | } 29 | }; -------------------------------------------------------------------------------- /Lecture-35(BinaryTrees)/BST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-35(BinaryTrees)/BST -------------------------------------------------------------------------------- /Lecture-35(BinaryTrees)/BST.cpp: -------------------------------------------------------------------------------- 1 | // BST.cpp 2 | #include 3 | using namespace std; 4 | 5 | class node 6 | { 7 | public: 8 | int data; 9 | node* left; 10 | node* right; 11 | node(int d) { 12 | data = d; 13 | left = right = NULL; 14 | } 15 | }; 16 | 17 | node* insertInBST(node* root, int d) { 18 | if (!root) { 19 | root = new node(d); 20 | return root; 21 | } 22 | 23 | if (root->data >= d) { 24 | root->left = insertInBST(root->left, d); 25 | } 26 | else { 27 | root->right = insertInBST(root->right, d); 28 | } 29 | return root; 30 | } 31 | // 8 3 10 1 6 14 4 7 13 -1 32 | node* createBST() { 33 | int data; 34 | cin >> data; 35 | if (data == -1) { 36 | return NULL; 37 | } 38 | node* root = NULL; 39 | while (data != -1) { 40 | root = insertInBST(root, data); 41 | 42 | cin >> data; 43 | } 44 | return root; 45 | } 46 | 47 | 48 | void preorder(node* root) { 49 | if (root == NULL) { 50 | return; 51 | } 52 | cout << root->data << " "; 53 | preorder(root->left); 54 | preorder(root->right); 55 | } 56 | 57 | void inorder(node* root) { 58 | if (root == NULL) { 59 | return; 60 | } 61 | inorder(root->left); 62 | cout << root->data << " "; 63 | inorder(root->right); 64 | } 65 | 66 | void postorder(node* root) { 67 | if (root == NULL) { 68 | return; 69 | } 70 | postorder(root->left); 71 | postorder(root->right); 72 | cout << root->data << " "; 73 | } 74 | 75 | int main() { 76 | 77 | node* root = createBST(); 78 | 79 | preorder(root); 80 | cout << endl; 81 | inorder(root); 82 | cout << endl; 83 | postorder(root); 84 | cout << endl; 85 | return 0; 86 | } 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /Lecture-35(BinaryTrees)/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-35(BinaryTrees)/Test -------------------------------------------------------------------------------- /Lecture-35(BinaryTrees)/input.txt: -------------------------------------------------------------------------------- 1 | 8 10 1 -1 -1 6 4 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1 -------------------------------------------------------------------------------- /Lecture-36(NST)/BST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-36(NST)/BST -------------------------------------------------------------------------------- /Lecture-36(NST)/PriorityQueue.cpp: -------------------------------------------------------------------------------- 1 | // PriorityQueue.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | 8 | return 0; 9 | } 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Lecture-36(NST)/input.txt: -------------------------------------------------------------------------------- 1 | 8 3 10 1 6 14 4 7 13 9 -1 -------------------------------------------------------------------------------- /Lecture-37(QPS)/Histogram.cpp: -------------------------------------------------------------------------------- 1 | // Histogram.cpp 2 | class Solution { 3 | public: 4 | int largestRectangleArea(vector& a) { 5 | stack s; 6 | vector next_smaller(a.size()); 7 | vector prev_smaller(a.size()); 8 | int n = a.size(); 9 | for (int i = 0; i < n; ++i) 10 | { 11 | while (!s.empty() and a[s.top()] > a[i]) { 12 | next_smaller[s.top()] = i; 13 | s.pop(); 14 | } 15 | s.push(i); 16 | } 17 | 18 | while (!s.empty()) { 19 | next_smaller[s.top()] = n; 20 | s.pop(); 21 | } 22 | 23 | for (int i = n - 1; i >= 0; --i) 24 | { 25 | while (!s.empty() and a[s.top()] > a[i]) { 26 | prev_smaller[s.top()] = i; 27 | s.pop(); 28 | } 29 | s.push(i); 30 | } 31 | 32 | while (!s.empty()) { 33 | prev_smaller[s.top()] = -1; 34 | s.pop(); 35 | } 36 | 37 | // for (int i = 0; i < n; ++i) 38 | // { 39 | // cout << next_smaller[i] << " "; 40 | // } 41 | // cout << endl; 42 | // for (int i = 0; i < n; ++i) 43 | // { 44 | // cout << prev_smaller[i] << " "; 45 | // } 46 | // cout << endl; 47 | int ans = 0; 48 | for (int i = 0; i < n; ++i) 49 | { 50 | int l = next_smaller[i] - prev_smaller[i] - 1; 51 | int h = a[i]; 52 | int area = l * h; 53 | ans = max(ans, area); 54 | } 55 | return ans; 56 | } 57 | }; 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /Lecture-37(QPS)/Test.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector nextGreaterElement(vector& a) { 4 | stack s; 5 | vector ng(a.size()); 6 | // for (int i = 0; i < n; ++i) 7 | // { 8 | // if (s.empty() || a[s.top] > a[i]) { 9 | // s.push(i); 10 | // } 11 | // else { 12 | // while (!s.empty() and a[s.top()] < a[i]) { 13 | // ng[s.top()] = i; 14 | // s.pop(); 15 | // } 16 | // s.push(i); 17 | // } 18 | // } 19 | for (int i = 0; i < n; ++i) 20 | { 21 | while (!s.empty() and a[s.top()] < a[i]) { 22 | ng[s.top()] = i; 23 | s.pop(); 24 | } 25 | s.push(i); 26 | } 27 | 28 | while (!s.empty()) { 29 | ng[s.top()] = -1; 30 | s.pop(); 31 | } 32 | 33 | } 34 | }; -------------------------------------------------------------------------------- /Lecture-38(Heaps)/Heap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-38(Heaps)/Heap -------------------------------------------------------------------------------- /Lecture-38(Heaps)/Heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class MinHeap 6 | { 7 | vector v; 8 | void heapify(int indx) { 9 | int left = 2 * indx; 10 | int right = left + 1; 11 | int temp = indx; 12 | if (leftv[left]) { 13 | temp = left; 14 | } 15 | if (rightv[right]) { 16 | temp = right; 17 | } 18 | if (temp != indx) { 19 | swap(v[temp], v[indx]); 20 | heapify(temp); 21 | } 22 | } 23 | public: 24 | MinHeap() { 25 | v.push_back(-1); // To block the zeroth index, since we aren't using it 26 | } 27 | 28 | void push(int d) { 29 | v.push_back(d); 30 | int c = v.size() - 1; 31 | int p = c / 2; 32 | while (c > 1 and v[p] > v[c]) { 33 | swap(v[p], v[c]); 34 | c = p; 35 | p /= 2; 36 | } 37 | } 38 | 39 | void pop() { 40 | // if (v.size() == 1) return; 41 | swap(v[1], v[v.size() - 1]); 42 | v.pop_back(); 43 | heapify(1); 44 | } 45 | 46 | bool empty() { 47 | return v.size() == 1; 48 | } 49 | 50 | int top() { 51 | return v[1]; 52 | } 53 | 54 | }; 55 | 56 | int main() { 57 | 58 | MinHeap h; 59 | h.push(1); 60 | h.push(2); 61 | h.push(5); 62 | h.push(3); 63 | h.push(4); 64 | h.push(0); 65 | 66 | while (!h.empty()) { 67 | cout << h.top() << " "; 68 | h.pop(); 69 | } 70 | cout << endl; 71 | 72 | 73 | return 0; 74 | } 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /Lecture-38(Heaps)/NewHeap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-38(Heaps)/NewHeap -------------------------------------------------------------------------------- /Lecture-38(Heaps)/NewHeap.cpp: -------------------------------------------------------------------------------- 1 | // NewHeap.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class meriClass { 7 | public: 8 | // Functor 9 | bool operator()(int a, int b) { 10 | return a > b; 11 | } 12 | }; 13 | 14 | 15 | class MinHeap 16 | { 17 | vector v; 18 | void heapify(int indx) { 19 | int left = 2 * indx; 20 | int right = left + 1; 21 | int temp = indx; 22 | if (leftv[left]) { 23 | temp = left; 24 | } 25 | if (rightv[right]) { 26 | temp = right; 27 | } 28 | if (temp != indx) { 29 | swap(v[temp], v[indx]); 30 | heapify(temp); 31 | } 32 | } 33 | public: 34 | MinHeap() { 35 | v.push_back(-1); // To block the zeroth index, since we aren't using it 36 | } 37 | 38 | void push(int d) { 39 | v.push_back(d); 40 | int c = v.size() - 1; 41 | int p = c / 2; 42 | meriClass fun; 43 | while (c > 1 and fun(v[p] , v[c])) { 44 | swap(v[p], v[c]); 45 | c = p; 46 | p /= 2; 47 | } 48 | } 49 | 50 | void pop() { 51 | // if (v.size() == 1) return; 52 | swap(v[1], v[v.size() - 1]); 53 | v.pop_back(); 54 | heapify(1); 55 | } 56 | 57 | bool empty() { 58 | return v.size() == 1; 59 | } 60 | 61 | int top() { 62 | return v[1]; 63 | } 64 | 65 | }; 66 | 67 | int main() { 68 | 69 | MinHeap h; 70 | h.push(1); 71 | h.push(2); 72 | h.push(5); 73 | h.push(3); 74 | h.push(4); 75 | h.push(0); 76 | 77 | while (!h.empty()) { 78 | cout << h.top() << " "; 79 | h.pop(); 80 | } 81 | cout << endl; 82 | 83 | 84 | return 0; 85 | } 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /Lecture-38(Heaps)/PriorityQueue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-38(Heaps)/PriorityQueue -------------------------------------------------------------------------------- /Lecture-38(Heaps)/PriorityQueue.cpp: -------------------------------------------------------------------------------- 1 | // PriorityQueue.cpp 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class meriClass { 8 | public: 9 | // Functor 10 | bool operator()(int a, int b) { 11 | return a < b; 12 | } 13 | }; 14 | 15 | int main() { 16 | priority_queue, meriClass > q; 17 | 18 | // priority_queue q; 19 | // priority_queue, greater > q; 20 | 21 | 22 | q.push(1); 23 | q.push(2); 24 | q.push(4); 25 | q.push(3); 26 | q.push(6); 27 | q.push(5); 28 | 29 | while (!q.empty()) { 30 | cout << q.top() << " "; 31 | q.pop(); 32 | } 33 | cout << endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Lecture-38(Heaps)/TopKElements: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-38(Heaps)/TopKElements -------------------------------------------------------------------------------- /Lecture-38(Heaps)/TopKElements.cpp: -------------------------------------------------------------------------------- 1 | // TopKElements.cpp 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void printQueue(priority_queue, greater > q) { 8 | while (!q.empty()) { 9 | cout << q.top() << " "; 10 | q.pop(); 11 | } 12 | cout << endl; 13 | } 14 | 15 | int main() { 16 | 17 | // k = 3 18 | 19 | // 1 2 3 0 0 -1 4 0 0 0 -1 6 7 8 -1 -4 -5 -6 -1 20 | // 1st time: 1 2 3 21 | // 2nd time: 2 3 4 22 | // 3rd time: 6 7 8 23 | // 4th time: 6 7 8 24 | 25 | priority_queue, greater > q; 26 | int data; 27 | int k = 3, cnt = 0; 28 | while (1) { 29 | cin >> data; 30 | if (data == -1) { 31 | // print top k elements till now from start 32 | printQueue(q); 33 | 34 | } else { 35 | if (cnt < k) { 36 | q.push(data); 37 | cnt++; 38 | } 39 | else { 40 | if (data > q.top()) { 41 | q.pop(); 42 | q.push(data); 43 | } 44 | } 45 | } 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /Lecture-39(Hashmaps)/Hashmap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-39(Hashmaps)/Hashmap -------------------------------------------------------------------------------- /Lecture-39(Hashmaps)/Hashmap.cpp: -------------------------------------------------------------------------------- 1 | // Hashmap.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | unordered_map h; 9 | // We need to insert a pair of key and value 10 | // 1st way of insertion 11 | pair p; 12 | p.first = "Mango"; 13 | p.second = 100; 14 | h.insert(p); 15 | 16 | // 2nd way of insertion 17 | pair p1("Kiwi", 50); 18 | h.insert(p1); 19 | 20 | // 3rd way of insertion 21 | h.insert({"Banana", 60}); 22 | h.insert({"Litchi", 60}); 23 | h.insert({"Peach", 60}); 24 | h.insert({"Guava", 60}); 25 | h.insert({"Ramfal", 60}); 26 | h.insert({"Sitafal", 60}); 27 | 28 | // 4th way of insertion 29 | h.insert(make_pair("Papaya", 90)); 30 | 31 | // for each pair p that belongs to hashmap h 32 | // for (pair p : h) { 33 | for (auto p : h) { 34 | cout << p.first << ", " << p.second << endl; 35 | } 36 | 37 | 38 | return 0; 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Lecture-39(Hashmaps)/Ordered_map: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-39(Hashmaps)/Ordered_map -------------------------------------------------------------------------------- /Lecture-39(Hashmaps)/Ordered_map.cpp: -------------------------------------------------------------------------------- 1 | // Hashmap.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | // Output is sorted according to key 9 | map h; // Insertion, deletion: O(logn), RedBlack Trees 10 | // We need to insert a pair of key and value 11 | // 1st way of insertion 12 | pair p; 13 | p.first = "Mango"; 14 | p.second = 100; 15 | h.insert(p); 16 | 17 | // 2nd way of insertion 18 | pair p1("Kiwi", 50); 19 | h.insert(p1); 20 | 21 | // 3rd way of insertion 22 | h.insert({"Banana", 60}); 23 | h.insert({"Litchi", 60}); 24 | h.insert({"Peach", 60}); 25 | h.insert({"Guava", 60}); 26 | h.insert({"Ramfal", 60}); 27 | h.insert({"Sitafal", 60}); 28 | 29 | // 4th way of insertion 30 | h.insert(make_pair("Papaya", 90)); 31 | 32 | // for each pair p that belongs to hashmap h 33 | // for (pair p : h) { 34 | for (auto p : h) { 35 | cout << p.first << ", " << p.second << endl; 36 | } 37 | 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Lecture-39(Hashmaps)/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-39(Hashmaps)/Test -------------------------------------------------------------------------------- /Lecture-39(Hashmaps)/hashmapInsertion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-39(Hashmaps)/hashmapInsertion -------------------------------------------------------------------------------- /Lecture-39(Hashmaps)/hashmapInsertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | unordered_map h; 8 | // We need to insert a pair of key and value 9 | // 1st way of insertion 10 | pair p; 11 | p.first = "Mango"; 12 | p.second = 100; 13 | h.insert(p); 14 | 15 | // 2nd way of insertion 16 | pair p1("Kiwi", 50); 17 | h.insert(p1); 18 | 19 | // 3rd way of insertion 20 | h.insert({"Banana", 60}); 21 | h.insert({"Litchi", 60}); 22 | h.insert({"Peach", 60}); 23 | h.insert({"Guava", 60}); 24 | h.insert({"Ramfal", 60}); 25 | h.insert({"Sitafal", 60}); 26 | 27 | // 4th way of insertion 28 | h.insert(make_pair("Papaya", 90)); 29 | 30 | for (int i = 0; i < h.bucket_count(); ++i) 31 | { 32 | cout << i << "-->"; 33 | for (auto it = h.begin(i) ; it != h.end(i) ; it++) { 34 | cout << "(" << it->first << ", " << it->second << ") "; 35 | } 36 | cout << endl; 37 | } 38 | 39 | 40 | 41 | return 0; 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Lecture-4/BitwiseOperators: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-4/BitwiseOperators -------------------------------------------------------------------------------- /Lecture-4/BitwiseOperators.cpp: -------------------------------------------------------------------------------- 1 | // BitwiseOperators.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | cout << "5&7: " << (5 & 7) << endl; 8 | cout << "5|7: " << (5 | 7) << endl; 9 | cout << "5|10: " << (5 | 10) << endl; 10 | cout << "500&600: " << (600 & 500) << endl; 11 | 12 | 13 | 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Lecture-4/CompoundAssignment.cpp: -------------------------------------------------------------------------------- 1 | // CompoundAssignment.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int a = 10; 7 | 8 | // a = a + 1; 9 | a += 1; 10 | 11 | // a = a -10; 12 | a -= 10; 13 | 14 | // a = a *10; 15 | a *= 10; 16 | 17 | // a = a >>1; 18 | a >>= 1; 19 | 20 | // a = a | b; 21 | a |= b; 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-4/Constants: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-4/Constants -------------------------------------------------------------------------------- /Lecture-4/Constants.cpp: -------------------------------------------------------------------------------- 1 | // Constants.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | // const float pi = 3.14; 8 | const float pi = 3.14; 9 | // We have to initialize the constant buckets that we make 10 | // pi = 3.14; 11 | 12 | cout << pi << endl; 13 | 14 | return 0; 15 | } 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Lecture-4/CountSetBits: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-4/CountSetBits -------------------------------------------------------------------------------- /Lecture-4/CountSetBits.cpp: -------------------------------------------------------------------------------- 1 | // CountSetBits.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, ans = 0; 8 | // cin >> n; 9 | n = 128; 10 | 11 | while (n > 0) { 12 | if ((n & 1) == 1) { 13 | ans ++; 14 | } 15 | 16 | n = n >> 1; 17 | } 18 | 19 | cout << "Set Bits: " << ans << endl; 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-4/ExplicitTypeConversion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-4/ExplicitTypeConversion -------------------------------------------------------------------------------- /Lecture-4/ExplicitTypeConversion.cpp: -------------------------------------------------------------------------------- 1 | // ExplicitTypeConversion.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | char ch = 'A'; 8 | int x = 67; 9 | 10 | cout << (int)ch << endl; 11 | cout << (char)x << endl; 12 | 13 | return 0; 14 | } 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Lecture-4/Macros: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-4/Macros -------------------------------------------------------------------------------- /Lecture-4/Macros.cpp: -------------------------------------------------------------------------------- 1 | // Macros.cpp 2 | #include 3 | using namespace std; 4 | // Syntax: #define MACRO_NAME MACRO__VAL 5 | #define NEWLINE endl 6 | #define PRINT cout<< 7 | #define PI 3.14+1 8 | int main() { 9 | // PRINT "Hello World!!" << NEWLINE; 10 | 11 | int r = 1; 12 | cout << 2 * PI * r << endl; 13 | 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Lecture-4/SizeOf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-4/SizeOf -------------------------------------------------------------------------------- /Lecture-4/SizeOf.cpp: -------------------------------------------------------------------------------- 1 | // SizeOf.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | cout << "Int size: " << sizeof(int) << endl; 8 | cout << "Float size: " << sizeof(float) << endl; 9 | cout << "bool size: " << sizeof(bool) << endl; 10 | cout << "char size: " << sizeof(char) << endl; 11 | cout << "double size: " << sizeof(double) << endl; 12 | cout << "double size: " << sizeof(10.11) << endl; 13 | cout << "Int size: " << sizeof(100) << endl; 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Lecture-4/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Lecture-40(QPS)/ConsecutiveOnes.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/max-consecutive-ones-iii/ 2 | class Solution { 3 | public: 4 | int longestOnes(vector& nums, int k) { 5 | int i, j, flips, n, ans; 6 | i = j = flips = ans = 0; 7 | n = nums.size(); 8 | 9 | while (j < n) { 10 | 11 | if (nums[j] == 0) flips++; 12 | while (flips > k) { 13 | if (nums[i] == 0) flips--; 14 | i++; 15 | } 16 | 17 | ans = max(ans, j - i + 1); 18 | j++; 19 | } 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /Lecture-40(QPS)/KDistinctChar.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 2 | class Solution { 3 | public: 4 | int lengthOfLongestSubstringKDistinct(string &s, int k) { 5 | int ans, i, j, d, n; 6 | ans = i = j = d = 0; 7 | n = s.size(); 8 | vector freq(256, 0); 9 | 10 | while (j < n) { 11 | if (freq[s[j]] == 0) d++; 12 | freq[s[j]]++; 13 | while (d > k) { 14 | freq[s[i]]--; 15 | if (freq[s[i]] == 0) d--; 16 | i++; 17 | } 18 | ans = max(ans, j - i + 1); 19 | j++; 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /Lecture-40(QPS)/LongestNonRepeatingChar.cpp: -------------------------------------------------------------------------------- 1 | // LongestNonRepeatingChar.cpp 2 | // https://leetcode.com/problems/longest-substring-without-repeating-characters/ 3 | class Solution { 4 | public: 5 | int lengthOfLongestSubstring(string &s) { 6 | vector freq(256, 0); 7 | int n = s.length(); 8 | int i = 0, j = 0, ans = 0; 9 | while (j < n) { 10 | char ch = s[j]; 11 | int indx = ch; 12 | freq[indx]++; 13 | // window shrink 14 | while (freq[s[j]] > 1) { 15 | char ch1 = s[i]; 16 | int k = ch1; 17 | freq[k]--; 18 | i++; 19 | } 20 | ans = max(ans, j - i + 1); 21 | j++; 22 | } 23 | return ans; 24 | } 25 | }; 26 | /* 27 | int lengthOfLongestSubstring(string &s) { 28 | vector freq(256, 0); 29 | int n = s.length(); 30 | int i = 0, j = 0, ans = 0; 31 | while (j < n) { 32 | freq[s[j]]++; 33 | // window shrink 34 | while (freq[s[j]] > 1) { 35 | freq[s[i]]--; 36 | i++; 37 | } 38 | ans = max(ans, j - i + 1); 39 | j++; 40 | } 41 | return ans; 42 | } 43 | */ -------------------------------------------------------------------------------- /Lecture-40(QPS)/MaxSlidingWindow.cpp: -------------------------------------------------------------------------------- 1 | // MaxSlidingWindow.cpp 2 | class Solution { 3 | public: 4 | vector maxSlidingWindow(vector& nums, int k) { 5 | deque q; 6 | vector ans; 7 | int n = nums.size(); 8 | 9 | for (int i = 0; i < n; ++i) 10 | { 11 | if (q.empty()) q.push_back(i); 12 | else { 13 | if (q.front() <= i - k) q.pop_front(); 14 | while (!q.empty() and nums[q.back()] <= nums[i]) 15 | q.pop_back(); 16 | q.push_back(i); 17 | } 18 | if (i >= k - 1) { 19 | ans.push_back(nums[q.front()]); 20 | } 21 | } 22 | 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /Lecture-40(QPS)/Test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-40(QPS)/Test.cpp -------------------------------------------------------------------------------- /Lecture-41(DP)/CoinExchange: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-41(DP)/CoinExchange -------------------------------------------------------------------------------- /Lecture-41(DP)/CoinExchange.cpp: -------------------------------------------------------------------------------- 1 | // CoinExchange.cpp 2 | #include 3 | using namespace std; 4 | 5 | int CoinExchange(int amount, int *deno, int n) { 6 | // base case 7 | if (amount == 0) { 8 | return 0; 9 | } 10 | 11 | // recursive case 12 | int ans = INT_MAX; 13 | for (int i = 0; i < n; ++i)// amt = 5, deno[] = {10,20,30}; 14 | { 15 | if (amount >= deno[i]) { 16 | int chotiAmount = amount - deno[i]; 17 | int chotiAmountCoins = CoinExchange(chotiAmount, deno, n); 18 | if (chotiAmountCoins != INT_MAX) { 19 | ans = min(chotiAmountCoins + 1, ans); 20 | } 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | 27 | int main() { 28 | 29 | int deno[] = {1, 7, 10}; 30 | int n = sizeof(deno) / sizeof(int); 31 | 32 | cout << CoinExchange(100, deno, n) << endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Lecture-41(DP)/MinStepsTo1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-41(DP)/MinStepsTo1 -------------------------------------------------------------------------------- /Lecture-41(DP)/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-41(DP)/Test -------------------------------------------------------------------------------- /Lecture-41(DP)/Test-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-41(DP)/Test-1 -------------------------------------------------------------------------------- /Lecture-41(DP)/Test-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | cout << "INT_MAX: " << INT_MAX << endl; 7 | cout << "INT_MAX+1: " << INT_MAX + 1 << endl; 8 | cout << "INT_MIN: " << INT_MIN << endl; 9 | cout << "INT_MIN-1: " << INT_MIN - 1 << endl; 10 | 11 | 12 | return 0; 13 | } 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Lecture-41(DP)/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fiboRecursion(int n) {// O(2^N) 5 | if (n <= 1) return n; 6 | 7 | return fiboRecursion(n - 1) + fiboRecursion(n - 2); 8 | } 9 | 10 | int fibo(int n, int *dp) { // O(N) 11 | if (n <= 1) { 12 | dp[n] = n; 13 | return n; 14 | } 15 | 16 | if (dp[n] != -1) { 17 | return dp[n]; 18 | } 19 | 20 | int ans = fibo(n - 1, dp) + fibo(n - 2, dp); 21 | dp[n] = ans; 22 | return ans; 23 | } 24 | 25 | int bottomUp(int n) { 26 | int* dp = new int[n + 1]; 27 | dp[0] = 0; 28 | dp[1] = 1; 29 | 30 | for (int i = 2; i <= n; ++i) 31 | { 32 | dp[i] = dp[i - 1] + dp[i - 2]; 33 | } 34 | 35 | for (int i = 0; i <= n; ++i) 36 | { 37 | cout << dp[i] << " "; 38 | } 39 | cout << endl; 40 | return dp[n]; 41 | } 42 | 43 | int main() { 44 | int dp[10000]; 45 | for (int i = 0; i < 10000; ++i) 46 | { 47 | dp[i] = -1; 48 | } 49 | 50 | cout << fibo(10, dp) << endl; 51 | for (int i = 0; i < 11; ++i) 52 | { 53 | cout << dp[i] << " "; 54 | } 55 | cout << endl; 56 | cout << bottomUp(10) << endl; 57 | // cout << fiboRecursion(100) << endl; 58 | 59 | return 0; 60 | } 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /Lecture-42(DP)/CoinExchange: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-42(DP)/CoinExchange -------------------------------------------------------------------------------- /Lecture-42(DP)/CoinExchange.cpp: -------------------------------------------------------------------------------- 1 | // CoinExchange.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int CoinExchange(int amount, int *deno, int n, int *dp) { 7 | // base case 8 | if (amount == 0) { 9 | return dp[amount] = 0; 10 | } 11 | 12 | if (dp[amount] != -1) { 13 | return dp[amount]; 14 | } 15 | 16 | // recursive case 17 | int ans = INT_MAX; 18 | for (int i = 0; i < n; ++i)// amt = 5, deno[] = {10,20,30}; 19 | { 20 | if (amount >= deno[i]) { 21 | int chotiAmount = amount - deno[i]; 22 | int chotiAmountCoins = CoinExchange(chotiAmount, deno, n, dp); 23 | if (chotiAmountCoins != INT_MAX) { 24 | ans = min(chotiAmountCoins + 1, ans); 25 | } 26 | } 27 | } 28 | 29 | return dp[amount] = ans; 30 | } 31 | 32 | int bottomUp(int *deno, int n, int amt) { 33 | vector dp(amt + 1, INT_MAX); 34 | dp[0] = 0; 35 | 36 | for (int rupay = 1; rupay <= amt; ++rupay) 37 | { 38 | for (int j = 0; j < n; ++j) 39 | { 40 | if (rupay >= deno[j]) { 41 | int kamRupay = rupay - deno[j]; 42 | dp[rupay] = min( 43 | dp[rupay], 44 | dp[kamRupay] + 1 45 | ); 46 | } 47 | } 48 | } 49 | 50 | for (int rupay = 0; rupay <= amt; ++rupay) 51 | { 52 | cout << dp[rupay] << " "; 53 | } 54 | cout << endl; 55 | return dp[amt]; 56 | } 57 | 58 | 59 | int main() { 60 | 61 | int deno[] = {1, 5, 7}; 62 | int n = sizeof(deno) / sizeof(int); 63 | 64 | int dp[10000]; 65 | memset(dp, -1, sizeof dp); 66 | cout << bottomUp(deno, n, 15) << endl; 67 | cout << CoinExchange(15, deno, n, dp) << endl; 68 | 69 | return 0; 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /Lecture-42(DP)/WineProblem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-42(DP)/WineProblem -------------------------------------------------------------------------------- /Lecture-43(DP)/Knapsack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-43(DP)/Knapsack -------------------------------------------------------------------------------- /Lecture-43(DP)/Knapsack.cpp: -------------------------------------------------------------------------------- 1 | // Knapsack.cpp 2 | #include 3 | using namespace std; 4 | 5 | int topDown(int *price, int *wt, int capacity, int n, int dp[][100]) { 6 | // base case 7 | if (capacity == 0 || n == 0) { 8 | return dp[n][capacity] = 0; 9 | } 10 | if (dp[n][capacity] != -1) { 11 | return dp[n][capacity]; 12 | } 13 | // recursive case 14 | int op1, op2; 15 | op1 = op2 = INT_MIN; 16 | 17 | if (capacity >= wt[n - 1]) { 18 | op1 = price[n - 1 ] + topDown(price, wt, capacity - wt[n - 1], n - 1); 19 | } 20 | op2 = topDown(price, wt, capacity, n - 1); 21 | return dp[n][capacity] = max(op2, op1); 22 | } 23 | 24 | int bottomUp(int *price, int *wt, int N, int capacity) { 25 | int dp[100][100] = {0}; 26 | for (int n = 1; n <= N; ++n) 27 | { 28 | for (int cap = 1; cap <= capacity; ++cap) 29 | { 30 | int op1, op2; 31 | op1 = op2 = INT_MIN; 32 | if (cap >= wt[n - 1]) { 33 | op1 = price[n - 1] + dp[n - 1][cap - wt[n - 1]]; 34 | } 35 | op2 = 0 + dp[n - 1][cap]; 36 | dp[n][cap] = max(op1, op2); 37 | } 38 | } 39 | 40 | for (int n = 0; n <= N; ++n) 41 | { 42 | for (int cap = 0; cap <= capacity; ++cap) 43 | { 44 | cout << dp[n][cap] << " "; 45 | } 46 | cout << endl; 47 | } 48 | 49 | return dp[N][capacity]; 50 | } 51 | 52 | int main() { 53 | 54 | int price[] = {7, 4, 4}; 55 | int wt[] = {3, 2, 2}; 56 | 57 | int capacity = 4; 58 | 59 | cout << topDown(price, wt, capacity, 3) << endl; 60 | cout << bottomUp(price, wt, 3, capacity) << endl; 61 | return 0; 62 | } 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Lecture-43(DP)/LCS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-43(DP)/LCS -------------------------------------------------------------------------------- /Lecture-44(Graphs)/GenericGraph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-44(Graphs)/GenericGraph -------------------------------------------------------------------------------- /Lecture-44(Graphs)/GenericGraph.cpp: -------------------------------------------------------------------------------- 1 | // GenericGraph.cpp 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Graph { 8 | public: 9 | unordered_map > adj; 10 | void addEdge(string u, string v, bool bidir = true) { 11 | adj[u].push_back(v); 12 | if (bidir == true) { 13 | adj[v].push_back(u); 14 | } 15 | } 16 | 17 | void print() { 18 | for (auto p : adj) { 19 | cout << p.first << "-->"; 20 | for (auto neighbour : p.second) { 21 | cout << neighbour << " "; 22 | } 23 | cout << endl; 24 | } 25 | } 26 | 27 | }; 28 | 29 | int main() { 30 | 31 | Graph g; 32 | g.addEdge("Vaibhav", "Lakshya"); 33 | g.addEdge("Yash", "Lakshya"); 34 | g.addEdge("Yash", "Prabhnoor"); 35 | g.addEdge("Varun", "Prabhnoor"); 36 | g.addEdge("Barbie", "Rajas"); 37 | g.addEdge("Barbie", "Prabhnoor"); 38 | 39 | g.print(); 40 | 41 | 42 | return 0; 43 | } 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Lecture-44(Graphs)/Graphs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-44(Graphs)/Graphs -------------------------------------------------------------------------------- /Lecture-44(Graphs)/Graphs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Graph { 6 | public: 7 | list *adj; 8 | int n; 9 | 10 | Graph(int s) { 11 | adj = new list[s]; 12 | n = s; 13 | } 14 | 15 | void addEdge(int u, int v, bool bidir = true) { 16 | adj[u].push_back(v); 17 | if (bidir) { 18 | adj[v].push_back(u); 19 | } 20 | } 21 | 22 | void print() { 23 | for (int i = 0; i < n; ++i) 24 | { 25 | cout << i << "-->"; 26 | for (auto neighbour : adj[i]) { 27 | cout << neighbour << " "; 28 | } 29 | cout << endl; 30 | } 31 | } 32 | }; 33 | 34 | int main() { 35 | 36 | Graph g(4); 37 | g.addEdge(0, 1); 38 | g.addEdge(0, 2); 39 | g.addEdge(1, 2); 40 | g.addEdge(3, 2); 41 | g.addEdge(0, 3); 42 | 43 | g.print(); 44 | 45 | return 0; 46 | } 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /Lecture-44(Graphs)/list: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-44(Graphs)/list -------------------------------------------------------------------------------- /Lecture-44(Graphs)/list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | list l; 8 | l.push_back(0); 9 | l.push_back(1); 10 | l.push_back(2); 11 | l.push_back(3); 12 | l.push_back(4); 13 | l.push_back(5); 14 | l.push_back(6); 15 | l.push_back(7); 16 | 17 | // l.remove(3); 18 | for (auto it = l.begin(); it != l.end() ; it++) { 19 | if (*it == 3) { 20 | l.insert(it, 100); 21 | break; 22 | } 23 | } 24 | 25 | 26 | for (auto data : l) { 27 | cout << data << "-->"; 28 | } 29 | cout << endl; 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lecture-44(Graphs)/untitled: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-44(Graphs)/untitled -------------------------------------------------------------------------------- /Lecture-45(Graphs)/Dijkstras: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-45(Graphs)/Dijkstras -------------------------------------------------------------------------------- /Lecture-45(Graphs)/GenericGraph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-45(Graphs)/GenericGraph -------------------------------------------------------------------------------- /Lecture-45(Graphs)/WeightedGraph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-45(Graphs)/WeightedGraph -------------------------------------------------------------------------------- /Lecture-45(Graphs)/WeightedGraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | template < typename T> 8 | class Graph { 9 | public: 10 | unordered_map > > adj; 11 | void addEdge(T u, T v, int dist, bool bidir = true) { 12 | adj[u].push_back({v, dist}); 13 | if (bidir) { 14 | adj[v].push_back({u, dist}); 15 | } 16 | } 17 | 18 | void print() { 19 | for (auto n : adj) { 20 | cout << n.first << "-->"; 21 | for (auto ch : n.second) { 22 | cout << "(" << ch.first << ", " << ch.second << ") "; 23 | } 24 | cout << endl; 25 | } 26 | } 27 | }; 28 | 29 | int main() { 30 | 31 | Graph g; 32 | g.addEdge("Amritsar", "Agra", 1); 33 | g.addEdge("Delhi", "Agra", 1); 34 | g.addEdge("Delhi", "Jaipur", 2); 35 | g.addEdge("Amritsar", "Jaipur", 4); 36 | g.addEdge("Mumbai", "Jaipur", 8); 37 | g.addEdge("Mumbai", "Bhopal", 3); 38 | g.addEdge("Agra", "Bhopal", 2); 39 | 40 | g.print(); 41 | 42 | 43 | return 0; 44 | } 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Lecture-45(Graphs)/set: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-45(Graphs)/set -------------------------------------------------------------------------------- /Lecture-45(Graphs)/set.cpp: -------------------------------------------------------------------------------- 1 | // set.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | // Properties of set 6 | // 1. Duplicate entries aren't allowed 7 | // 2. Data will get automatically sorted in set 8 | 9 | int main() { 10 | 11 | set s; 12 | s.insert(5); 13 | s.insert(3); 14 | s.insert(2); 15 | s.insert(4); 16 | s.insert(1); 17 | 18 | // printing the set 19 | cout << "Print output: "; 20 | for (auto e : s) { 21 | cout << e << " "; 22 | } 23 | cout << endl; 24 | 25 | // Find or search 26 | auto address = s.find(40); 27 | if (address != s.end()) { 28 | cout << "Found: " << *address << endl; 29 | } 30 | else { 31 | cout << "Not Found\n"; 32 | } 33 | 34 | // Deletion in set 35 | while (!s.empty()) { 36 | cout << *s.begin() << " "; 37 | s.erase(s.begin()); 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Lecture-45(Graphs)/set_Pair: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-45(Graphs)/set_Pair -------------------------------------------------------------------------------- /Lecture-45(Graphs)/set_Pair.cpp: -------------------------------------------------------------------------------- 1 | // set_Pair.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | set > s; 9 | s.insert({4, 2}); 10 | s.insert({6, 1}); 11 | s.insert({3, 20}); 12 | s.insert({2, 12}); 13 | s.insert({1, 9}); 14 | 15 | auto add = s.find({4, 2}); 16 | // (*add).second = 20; 17 | // cout << (*add).first << ", " << (*add).second << endl; 18 | s.erase(add); 19 | s.insert({4, 20}); 20 | 21 | 22 | while (!s.empty()) { 23 | auto address = s.begin(); 24 | auto p = *address; 25 | 26 | cout << p.first << ", " << p.second << endl; 27 | 28 | s.erase(address); 29 | } 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lecture-5/CheckPrime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/CheckPrime -------------------------------------------------------------------------------- /Lecture-5/CheckPrime.cpp: -------------------------------------------------------------------------------- 1 | // CheckPrime.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, i; 8 | cin >> n; 9 | bool isPrime = true; 10 | 11 | for (i = 2; i < n; ++i) 12 | { 13 | if (n % i == 0) { 14 | isPrime = false; 15 | } 16 | } 17 | 18 | if (isPrime == true) { 19 | cout << n << endl; 20 | } 21 | 22 | // if (isPrime == true) { 23 | // cout << "Prime" << endl; 24 | // } 25 | // else { 26 | // cout << "Not Prime" << endl; 27 | // } 28 | 29 | // int x; 30 | // cin >> x; 31 | // cout << 2 * x << endl; 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Lecture-5/ForLoop: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/ForLoop -------------------------------------------------------------------------------- /Lecture-5/ForLoop.cpp: -------------------------------------------------------------------------------- 1 | // ForLoop.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int i = 1; 8 | while (i <= 3) { 9 | cout << i << " "; 10 | i++; 11 | } 12 | cout << endl; 13 | // For loop: 1st way 14 | for (i = 1 ; i <= 3 ; i++) { 15 | cout << i << " "; 16 | } 17 | cout << endl; 18 | 19 | 20 | // For loop: 2nd way 21 | i = 1; 22 | for ( ; i <= 6 ; ) { 23 | cout << i << " "; 24 | 25 | i++; 26 | } 27 | cout << endl; 28 | 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture-5/PrintAllPrimes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/PrintAllPrimes -------------------------------------------------------------------------------- /Lecture-5/PrintAllPrimes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, i; 6 | int number; 7 | bool isPrime; 8 | cin >> number; 9 | 10 | for (n = 2; n <= number; ++n) 11 | { 12 | isPrime = true; 13 | for (i = 2; i < n; ++i) 14 | { 15 | if (n % i == 0) { 16 | isPrime = false; 17 | } 18 | } 19 | 20 | if (isPrime == true) { 21 | cout << n << " "; 22 | } 23 | 24 | } 25 | cout << endl; 26 | return 0; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Lecture-5/PrintPattern: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/PrintPattern -------------------------------------------------------------------------------- /Lecture-5/PrintPattern-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/PrintPattern-1 -------------------------------------------------------------------------------- /Lecture-5/PrintPattern-1.cpp: -------------------------------------------------------------------------------- 1 | // PrintPattern.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, row, no, i; 8 | cin >> n; 9 | 10 | for (row = 1 ; row <= n ; row++) { 11 | if (row % 2 == 0) { 12 | no = 0; 13 | } 14 | else { 15 | no = 1; 16 | } 17 | 18 | for (i = 1; i <= row ; i++) { 19 | cout << no << " "; 20 | no = 1 - no; 21 | } 22 | 23 | cout << endl; 24 | } 25 | 26 | 27 | return 0; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Lecture-5/PrintPattern.cpp: -------------------------------------------------------------------------------- 1 | // PrintPattern.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, row, no, i; 8 | cin >> n; 9 | 10 | for (row = 1; row <= n; ++row) 11 | { 12 | if (row % 2 == 0) { 13 | no = 0; 14 | } 15 | else { 16 | no = 1; 17 | } 18 | 19 | for (i = 1; i <= row; ++i) 20 | { 21 | cout << no << " "; 22 | no = 1 - no; 23 | } 24 | 25 | cout << endl; 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Lecture-5/ReverseNumber: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/ReverseNumber -------------------------------------------------------------------------------- /Lecture-5/ReverseNumber.cpp: -------------------------------------------------------------------------------- 1 | // ReverseNumber.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, ans = 0, digit; 7 | cin >> n; 8 | for (; n != 0; n /= 10) 9 | { 10 | digit = n % 10; 11 | ans = ans * 10 + digit; 12 | } 13 | // while (n != 0) { 14 | // digit = n % 10; 15 | // ans = ans * 10 + digit; 16 | 17 | // n /= 10; 18 | // } 19 | cout << "Reverse Number: " << ans << endl; 20 | cout << ans + 10 << endl; 21 | 22 | 23 | /// PRINT THE REVERSE 24 | 25 | // int n; 26 | // cin >> n; 27 | // for ( ; n != 0; n = n / 10) 28 | // { 29 | // cout << n % 10; 30 | // } 31 | // while (n != 0) { 32 | // cout << n % 10; 33 | 34 | // n /= 10; 35 | // } 36 | cout << endl; 37 | 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Lecture-5/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | 8 | return 0; 9 | } 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Lecture-5/UniqueNumber-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/UniqueNumber-1 -------------------------------------------------------------------------------- /Lecture-5/UniqueNumber-1.cpp: -------------------------------------------------------------------------------- 1 | // UniqueNumber - 1.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, no, i; 8 | cin >> n; 9 | int ans = 0; 10 | i = 1; 11 | while (i <= n) { 12 | cin >> no; 13 | // cout << no << endl; 14 | ans = ans ^ no; 15 | 16 | i++; 17 | } 18 | 19 | cout << "Unique Number: " << ans << endl; 20 | 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-5/Yash: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-5/Yash -------------------------------------------------------------------------------- /Lecture-5/Yash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[8] = {1, 1, 1, 0, 1, 1, 1, 1}; 7 | 8 | int old[10] = {0}, newa[10] = {0}; 9 | for (int i = 1; i <= 8; ++i) 10 | { 11 | old[i] = a[i - 1]; 12 | } 13 | 14 | for (int i = 0; i < 10; ++i) 15 | { 16 | cout << old[i] << " "; 17 | } 18 | cout << endl; 19 | int days = 2; 20 | while (days--) { 21 | for (int i = 1; i <= 8; ++i) 22 | { 23 | if (old[i - 1] == old[i + 1]) { 24 | newa[i] = 0; 25 | } 26 | else { 27 | newa[i] = 1; 28 | } 29 | } 30 | 31 | for (int i = 1; i <= 8; ++i) 32 | { 33 | old[i] = newa[i]; 34 | } 35 | cout << "Day: " << days << endl; 36 | for (int i = 0; i <= 9; ++i) 37 | { 38 | cout << old[i] << " "; 39 | } cout << endl; 40 | 41 | } 42 | 43 | for (int i = 1; i <= 8; ++i) 44 | { 45 | cout << old[i] << " "; 46 | } cout << endl; 47 | 48 | return 0; 49 | } 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /Lecture-6/AsciiCode: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-6/AsciiCode -------------------------------------------------------------------------------- /Lecture-6/AsciiCode.cpp: -------------------------------------------------------------------------------- 1 | // AsciiCode.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | cout << '1' << endl; 8 | 9 | int a = '1' - '0'; 10 | cout << a << endl; 11 | 12 | return 0; 13 | } 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Lecture-6/CountAllCharacters: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-6/CountAllCharacters -------------------------------------------------------------------------------- /Lecture-6/CountAllCharacters.cpp: -------------------------------------------------------------------------------- 1 | // CountAllCharacters.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int alphabets, digits, special, spaces; 8 | alphabets = digits = special = spaces = 0; 9 | char ch; 10 | ch = cin.get(); 11 | while (ch != '$') { 12 | // work 13 | if (ch >= '0' and ch <= '9') { 14 | digits++; 15 | } 16 | else if (ch == ' ' || ch == '\n' || ch == '\t') { 17 | spaces ++; 18 | } 19 | else if ( (ch >= 'a' and ch <= 'z') || (ch >= 'A' && ch <= 'Z' ) ) { 20 | alphabets ++; 21 | } 22 | else { 23 | special++; 24 | } 25 | 26 | ch = cin.get(); 27 | } 28 | 29 | cout << "Number of alphabets: " << alphabets << endl; 30 | cout << "Number of digits: " << digits << endl; 31 | cout << "Number of spaces: " << spaces << endl; 32 | cout << "Number of special: " << special << endl; 33 | 34 | 35 | 36 | 37 | 38 | return 0; 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Lecture-6/CountCharacters: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-6/CountCharacters -------------------------------------------------------------------------------- /Lecture-6/CountCharacters.cpp: -------------------------------------------------------------------------------- 1 | // CountCharacters.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | char ch; 8 | int cnt = 0; 9 | // To read whitespace characters 10 | // we use, 11 | // char ch; 12 | // ch = cin.get(); 13 | 14 | // instead of cin >> ch; 15 | 16 | // cin >> ch; // Intialization 17 | ch = cin.get(); 18 | 19 | while (ch != '$') { // Condition check 20 | cnt++; 21 | 22 | // cin >> ch; // Updation 23 | ch = cin.get(); 24 | } 25 | 26 | cout << "Number of characters: " << cnt << endl; 27 | return 0; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /Lecture-6/PatternStar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-6/PatternStar -------------------------------------------------------------------------------- /Lecture-6/PatternStar.cpp: -------------------------------------------------------------------------------- 1 | // PatternStar.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, i, row; 8 | cin >> n; 9 | for (row = 1 ; row <= n ; row++) { 10 | // Work 11 | // 1. print row times star 12 | for (i = 1; i <= row ; i++) { 13 | cout << '*'; 14 | } 15 | // 2. print ' ' 16 | cout << ' '; 17 | // 3. print n-row+1 times star 18 | for (i = 1 ; i <= n - row + 1 ; i++) { 19 | cout << '*'; 20 | } 21 | // 4. print ' ' 22 | cout << ' '; 23 | // 5. print n-row+1 times star 24 | for (i = 1 ; i <= n - row + 1 ; i++) { 25 | cout << '*'; 26 | } 27 | // 6. print ' ' 28 | cout << ' '; 29 | // 7. print row times star 30 | for (i = 1; i <= row ; i++) { 31 | cout << '*'; 32 | } 33 | cout << endl; 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Lecture-6/Scopes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-6/Scopes -------------------------------------------------------------------------------- /Lecture-6/Scopes.cpp: -------------------------------------------------------------------------------- 1 | // Scopes.cpp 2 | #include 3 | using namespace std; 4 | 5 | int a = 1000; 6 | 7 | int main() { 8 | cout << a << endl; 9 | int a = 1; 10 | 11 | if (a > 0) { 12 | int a = 10; 13 | cout << a++ << endl; 14 | cout << "Global a: " << ::a << endl; 15 | } 16 | ::a = ::a + 10; 17 | cout << ::a << endl; 18 | cout << a << endl; 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Lecture-6/Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-6/Test -------------------------------------------------------------------------------- /Lecture-6/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char ch; 6 | // cin >> ch; 7 | ch = cin.get(); 8 | 9 | if (ch == ' ' || ch == '\n' || ch == '\t') { 10 | cout << "White space character" << endl; 11 | } 12 | else { 13 | cout << "Other character: " << ch << endl; 14 | } 15 | 16 | cout << ch; 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lecture-7/ArrayInitialization: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/ArrayInitialization -------------------------------------------------------------------------------- /Lecture-7/ArrayInitialization.cpp: -------------------------------------------------------------------------------- 1 | // ArrayInitialization.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | // Initialization 8 | // int a[50] = {1, 2, 3, 4, 5}; 9 | // int a[] = {1, 2, 3, 4, 5};// Sirf aur sirf initialization ke time 10 | // hum number of buckets batana skip kar skte h 11 | 12 | int a[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 13 | int n = sizeof(a) / sizeof(int); 14 | 15 | for (int i = 0; i < n; ++i) 16 | { 17 | cout << a[i] << " "; 18 | } 19 | 20 | cout << endl; 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Lecture-7/Arrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/Arrays -------------------------------------------------------------------------------- /Lecture-7/Arrays.cpp: -------------------------------------------------------------------------------- 1 | // Arrays.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[5]; 8 | 9 | // a[0] = 1; 10 | // a[1] = 2; 11 | // a[2] = 3; 12 | // a[3] = 4; 13 | // a[4] = 5; 14 | for (int i = 0; i <= 4; ++i) 15 | { 16 | a[i] = i + 1; 17 | } 18 | 19 | // cout << a[0] << " "; 20 | // cout << a[1] << " "; 21 | // cout << a[2] << " "; 22 | // cout << a[3] << " "; 23 | // cout << a[4] << " "; 24 | for (int i = 0; i <= 4; ++i) 25 | { 26 | cout << a[i] << " "; 27 | } 28 | 29 | cout << endl; 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Lecture-7/BubbleSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/BubbleSort -------------------------------------------------------------------------------- /Lecture-7/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | // BubbleSort.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = {5, 4, 6, 2, 1}, temp; 8 | int n = sizeof(a) / sizeof(int); 9 | for (int i = 0; i < n; ++i) 10 | { 11 | cout << a[i] << " "; 12 | } 13 | cout << endl; 14 | 15 | for (int i = 0; i < n - 1; ++i) 16 | { 17 | for (int j = 0; j <= n - 2 - i; ++j) 18 | { 19 | if (a[j] > a[j + 1]) { 20 | // swap a[j] and a[j+1] 21 | temp = a[j]; 22 | a[j] = a[j + 1]; 23 | a[j + 1] = temp; 24 | } 25 | } 26 | } 27 | 28 | for (int i = 0; i < n; ++i) 29 | { 30 | cout << a[i] << " "; 31 | } 32 | cout << endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Lecture-7/Directions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/Directions -------------------------------------------------------------------------------- /Lecture-7/Directions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | char ch; 7 | int x = 0, y = 0; 8 | ch = cin.get(); // Initialization 9 | while (ch != '\n') { // Condition Check 10 | // Work 11 | 12 | if (ch == 'N') { 13 | y++; 14 | } 15 | else if (ch == 'S') { 16 | y--; 17 | } 18 | else if (ch == 'W') { 19 | x--; 20 | } 21 | else if (ch == 'E') { 22 | x++; 23 | } 24 | 25 | ch = cin.get(); // Updation 26 | } 27 | 28 | // find the quadrant and print the path 29 | if (x >= 0 and y >= 0) { // 1st quadrant 30 | // print x times 'E' and then y times 'N' 31 | for (int i = 0; i < x; ++i) 32 | { 33 | cout << "E"; 34 | } 35 | 36 | for (int i = 0; i < y ; ++i) 37 | { 38 | cout << "N"; 39 | } 40 | 41 | } 42 | else if (x <= 0 and y >= 0) { // 2nd quadrant 43 | // print y times 'N' and x times 'W' 44 | for (int i = 0; i < y ; ++i) 45 | { 46 | cout << "N"; 47 | } 48 | 49 | x = x * (-1); // because x is negative lets make it +ve 50 | for (int i = 0; i < x; ++i) 51 | { 52 | cout << "W"; 53 | } 54 | } 55 | else if (x <= 0 and y <= 0) { // 3rd Quadrant 56 | // print y times 'S' and x times 'W' 57 | y *= -1; 58 | for (int i = 0; i < y ; ++i) 59 | { 60 | cout << "S"; 61 | } 62 | 63 | x *= -1; 64 | for (int i = 0; i < x ; ++i) 65 | { 66 | cout << "W"; 67 | } 68 | } 69 | else { // 4th Quadrant 70 | // print x times 'E' and then y times 'S' 71 | for (int i = 0; i < x ; ++i) 72 | { 73 | cout << "E"; 74 | } 75 | 76 | y *= -1; 77 | for (int i = 0; i < y ; ++i) 78 | { 79 | cout << "S"; 80 | } 81 | } 82 | 83 | 84 | cout << endl; 85 | 86 | return 0; 87 | } 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /Lecture-7/FindLargest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/FindLargest -------------------------------------------------------------------------------- /Lecture-7/FindLargest.cpp: -------------------------------------------------------------------------------- 1 | // FindLargest.cpp 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | int a[] = {1, 2, 13, 4, 5}; 9 | int n = sizeof(a) / sizeof(int); 10 | 11 | int l = INT_MIN; 12 | 13 | for (int i = 0; i < n; ++i) 14 | { 15 | if (a[i] > l) { 16 | l = a[i]; 17 | } 18 | } 19 | cout << "Largest: " << l << endl; 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lecture-7/InsertionSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/InsertionSort -------------------------------------------------------------------------------- /Lecture-7/InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | // InsertionSort.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = {7, 6, 8, 4, 3, 2, 0, 1}, j, i, HPC; 8 | int n = sizeof(a) / sizeof(int); 9 | for (int i = 0; i < n; ++i) 10 | { 11 | cout << a[i] << " "; 12 | } 13 | cout << endl; 14 | 15 | for (i = 1; i < n; ++i) 16 | { 17 | HPC = a[i]; 18 | for (j = i - 1; j >= 0; --j) 19 | { 20 | if (meraCompare(a[j], HPC)) { 21 | a[j + 1] = a[j]; 22 | } 23 | else { 24 | break; 25 | } 26 | } 27 | a[j + 1] = HPC; 28 | } 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | cout << a[i] << " "; 33 | } 34 | cout << endl; 35 | 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Lecture-7/LinearSearch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/LinearSearch -------------------------------------------------------------------------------- /Lecture-7/LinearSearch-1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/LinearSearch-1 -------------------------------------------------------------------------------- /Lecture-7/LinearSearch-1.cpp: -------------------------------------------------------------------------------- 1 | // LinearSearch - 1.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = {1, 2, 3, 4, 5}, i ; 8 | int n = sizeof(a) / sizeof(int); 9 | int key = 0; 10 | for (i = 0; i < n; ++i) 11 | { 12 | if (a[i] == key) { 13 | cout << "Key found at: " << i << endl; 14 | break; 15 | } 16 | } 17 | 18 | if (i == n) { 19 | cout << "Key not found" << endl; 20 | } 21 | 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-7/LinearSearch.cpp: -------------------------------------------------------------------------------- 1 | // LinearSearch.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = {1, 2, 3, 4, 5}; 8 | int n = sizeof(a) / sizeof(int); 9 | 10 | int key = 40; 11 | bool keyMilli = false; 12 | for (int i = 0; i < n; ++i) 13 | { 14 | if (a[i] == key) { 15 | cout << "Key found at: " << i << endl; 16 | keyMilli = true; 17 | } 18 | } 19 | 20 | if (keyMilli == false) { 21 | cout << "Key not found" << endl; 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Lecture-7/ReverseNumbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/ReverseNumbers -------------------------------------------------------------------------------- /Lecture-7/ReverseNumbers.cpp: -------------------------------------------------------------------------------- 1 | // ReverseNumbers.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[1000], n; 8 | cout << "Enter N(Max: 1000) "; 9 | cin >> n; 10 | 11 | for (int i = 0; i < n; ++i) 12 | { 13 | a[i] = i + 1; 14 | } 15 | 16 | for (int i = n - 1; i >= 0; --i) 17 | { 18 | cout << a[i] << " "; 19 | } 20 | cout << endl; 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Lecture-7/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Lecture-7/UserInput: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-7/UserInput -------------------------------------------------------------------------------- /Lecture-7/UserInput.cpp: -------------------------------------------------------------------------------- 1 | // UserInput.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[1000], n; 8 | cout << "Enter N(Max: 1000) "; 9 | cin >> n; 10 | 11 | for (int i = 0; i < n; ++i) 12 | { 13 | cin >> a[i]; 14 | } 15 | 16 | for (int i = 0; i < n; ++i) 17 | { 18 | cout << a[i] << " "; 19 | } 20 | 21 | cout << endl; 22 | 23 | 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Lecture-7/doWhile.cpp: -------------------------------------------------------------------------------- 1 | doWhile.cpp -------------------------------------------------------------------------------- /Lecture-8/CelAndFah: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-8/CelAndFah -------------------------------------------------------------------------------- /Lecture-8/CelAndFah.cpp: -------------------------------------------------------------------------------- 1 | // CelAndFah.cpp 2 | #include 3 | using namespace std; 4 | 5 | void printTable(int init, int fval, int step) { 6 | int f, c; 7 | f = init; 8 | while (f <= fval) { 9 | // c = (5 / 9.0) * (f - 32); 10 | c = 5 * (f - 32) / 9; 11 | 12 | cout << f << " " << c << endl; 13 | 14 | 15 | f = f + step; 16 | } 17 | 18 | } 19 | 20 | int main() { 21 | int init, fval, step; 22 | cin >> init >> fval >> step; 23 | 24 | 25 | printTable(init, fval, step); 26 | cout << endl; 27 | printTable(init, fval, step); 28 | 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture-8/Functions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-8/Functions -------------------------------------------------------------------------------- /Lecture-8/Functions.cpp: -------------------------------------------------------------------------------- 1 | // Functions.cpp 2 | #include 3 | using namespace std; 4 | 5 | // syntax: 6 | // data_type function_name(arguments) { 7 | // Work 8 | // } 9 | 10 | void HelloWorld() { 11 | cout << "Hello World!" << endl; 12 | } 13 | 14 | void printAddition(int a, int b) { 15 | cout << "printing via function: " << a + b << endl; 16 | } 17 | 18 | int add(int x, int y) { 19 | int ans = x + y; 20 | 21 | return ans; // Return the answer jisne bhi maanga usse 22 | } 23 | 24 | void printOddEven(int result) { 25 | if (result % 2 == 0) { 26 | cout << "Even\n"; 27 | } 28 | else { 29 | cout << "Odd\n"; 30 | } 31 | } 32 | 33 | 34 | int main() { 35 | HelloWorld(); // Invoking or Calling 36 | int a = 10, b = 20; 37 | // printAddition(a, b); 38 | int result = add(a, b); 39 | cout << "Sum: " << result << endl; 40 | printOddEven(result); 41 | 42 | 43 | 44 | 45 | 46 | 47 | return 0; 48 | } 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Lecture-8/SelectionSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-8/SelectionSort -------------------------------------------------------------------------------- /Lecture-8/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {5, 6, 4, 3, 7, 8, 9, 0}; 7 | int n = sizeof(a) / sizeof(int); 8 | for (int i = 0; i < n; ++i) 9 | { 10 | cout << a[i] << " "; 11 | } 12 | cout << endl; 13 | 14 | for (int i = 0; i < n - 1; ++i) 15 | { 16 | int mi = i; 17 | for (int j = i + 1; j < n; ++j) 18 | { 19 | if (a[j] < a[mi]) { 20 | mi = j; 21 | } 22 | } 23 | 24 | // swap ai, ami 25 | int temp = a[i]; 26 | a[i] = a[mi]; 27 | a[mi] = temp; 28 | } 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | cout << a[i] << " "; 33 | } 34 | cout << endl; 35 | 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Lecture-8/SwitchCase: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-8/SwitchCase -------------------------------------------------------------------------------- /Lecture-8/SwitchCase.cpp: -------------------------------------------------------------------------------- 1 | // SwitchCase.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | char ch; 8 | cin >> ch; 9 | switch (ch) { 10 | case 'n': 11 | case 'N': 12 | cout << "North\n"; 13 | break; 14 | case 'e': 15 | case 'E': 16 | cout << "East\n"; 17 | break; 18 | case 's': 19 | case 'S': 20 | cout << "South\n"; 21 | break; 22 | case 'w': 23 | case 'W': 24 | cout << "West\n"; 25 | break; 26 | default: 27 | cout << "Galat direction\n"; 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture-8/TernaryOperator: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-8/TernaryOperator -------------------------------------------------------------------------------- /Lecture-8/TernaryOperator.cpp: -------------------------------------------------------------------------------- 1 | // TernaryOperator.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | // syntax: 8 | // condition ? work1 : work2; // condition: true(work1), false(work2) 9 | 10 | int a = 10; 11 | bool b; 12 | 13 | a > 0 ? cout << "Hello" << endl : cout << "World" << endl ; 14 | 15 | b = ( (a > 0) ? true : false ); 16 | b == true ? cout << "A is greater than zero\n" : cout << "A is smaller than 0\n"; 17 | 18 | // if (a > 0) { 19 | // cout << "Hello" << endl; 20 | // } 21 | // else { 22 | // cout << "World" << endl; 23 | // } 24 | 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Lecture-8/doWhile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-8/doWhile -------------------------------------------------------------------------------- /Lecture-8/doWhile.cpp: -------------------------------------------------------------------------------- 1 | // doWhile.cpp 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int i = 1; 8 | do { 9 | // Work 10 | cout << i << " "; 11 | 12 | i++; 13 | } while (i < 0); 14 | 15 | cout << endl; 16 | return 0; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Lecture-9/ArrayFunctions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/ArrayFunctions -------------------------------------------------------------------------------- /Lecture-9/ArrayFunctions.cpp: -------------------------------------------------------------------------------- 1 | // ArrayFunctions.cpp 2 | #include 3 | using namespace std; 4 | 5 | // void printArray(int a[5], int n) { 6 | void printArray(int a[], int n) { 7 | cout << "Printing Array: "; 8 | for (int i = 0; i < n; ++i) 9 | { 10 | cout << a[i] << " "; 11 | } 12 | cout << endl; 13 | } 14 | 15 | int main() { 16 | 17 | // int a[] = {1, 2, 3, 4, 5}; 18 | int a[] = {1, 2, 3, 4, 5}; 19 | int n = sizeof(a) / sizeof(int); 20 | 21 | printArray(a, n); 22 | 23 | 24 | return 0; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Lecture-9/ArraySum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/ArraySum -------------------------------------------------------------------------------- /Lecture-9/ArraySum.cpp: -------------------------------------------------------------------------------- 1 | // ArraySum.cpp 2 | #include 3 | using namespace std; 4 | 5 | int ArraySum(int a[], int n) { 6 | int sum = 0; 7 | for (int i = 0; i < n; ++i) 8 | { 9 | sum += a[i]; 10 | } 11 | return sum; 12 | } 13 | 14 | int main() { 15 | 16 | int a[] = {1, 2, 3, 4, 5}; 17 | int n = sizeof(a) / sizeof(int); 18 | 19 | int ans = ArraySum(a, n); 20 | cout << "Sum is: " << ans << endl; 21 | 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Lecture-9/BinarySearch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/BinarySearch -------------------------------------------------------------------------------- /Lecture-9/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | // BinarySearch.cpp 2 | #include 3 | using namespace std; 4 | int BinarySearch(int a[], int n, int key) { 5 | int s = 0, e = n - 1, m; 6 | 7 | while (s <= e) { 8 | m = (s + e) / 2; 9 | if (a[m] == key) { 10 | return m; 11 | } 12 | else if (key > a[m]) { 13 | s = m + 1; 14 | } 15 | else { 16 | e = m - 1; 17 | } 18 | } 19 | // s>e, key is not found 20 | return -1; 21 | } 22 | 23 | int main() { 24 | 25 | int a[] = {1, 2, 3, 4, 5}; 26 | int n = sizeof(a) / sizeof(int); 27 | int key = 0; 28 | int ans = BinarySearch(a, n, key); 29 | 30 | if (ans == -1) { 31 | cout << "Not Found\n"; 32 | } 33 | else { 34 | cout << "Key Found: " << ans << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Lecture-9/CallByValRef: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/CallByValRef -------------------------------------------------------------------------------- /Lecture-9/CallByValRef.cpp: -------------------------------------------------------------------------------- 1 | // CallByValRef.cpp 2 | #include 3 | using namespace std; 4 | 5 | void update(int &x) { 6 | x = x + 1; 7 | } 8 | 9 | int main() { 10 | 11 | int a = 1; 12 | cout << "Before update: " << a << endl; 13 | update(a); 14 | cout << "After update: " << a << endl; 15 | 16 | 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lecture-9/CelAndFah: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/CelAndFah -------------------------------------------------------------------------------- /Lecture-9/CelAndFah.cpp: -------------------------------------------------------------------------------- 1 | // CelAndFah.cpp 2 | #include 3 | using namespace std; 4 | 5 | void printTable(int init, int fval, int step) { 6 | int f, c; 7 | f = init; 8 | while (f <= fval) { 9 | // c = (5 / 9.0) * (f - 32); 10 | c = 5 * (f - 32) / 9; 11 | 12 | cout << f << " " << c << endl; 13 | 14 | 15 | f = f + step; 16 | } 17 | } 18 | 19 | int main() { 20 | 21 | int init, fval, step; 22 | cin >> init >> fval >> step; 23 | 24 | printTable(init, fval, step); 25 | 26 | 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Lecture-9/CheckPrime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/CheckPrime -------------------------------------------------------------------------------- /Lecture-9/CheckPrime.cpp: -------------------------------------------------------------------------------- 1 | // CheckPrime.cpp 2 | #include 3 | using namespace std; 4 | 5 | bool CheckPrime(int n) { 6 | for (int i = 2; i <= n - 1 ; i++) { 7 | if (n % i == 0) { 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | 14 | int main() { 15 | 16 | int n; 17 | cin >> n; 18 | 19 | if (CheckPrime(n)) { 20 | cout << "Prime\n"; 21 | } 22 | else { 23 | cout << "Not Prime\n"; 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Lecture-9/Factorial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/Factorial -------------------------------------------------------------------------------- /Lecture-9/Factorial.cpp: -------------------------------------------------------------------------------- 1 | // Factorial.cpp 2 | #include 3 | using namespace std; 4 | 5 | int fact(int n) { 6 | int ans = 1; 7 | for (int i = 1; i <= n; ++i) 8 | { 9 | ans = ans * i; 10 | } 11 | return ans; 12 | } 13 | 14 | int nCr(int n, int r) { 15 | int fact_n = fact(n); 16 | int fact_r = fact(r); 17 | int fact_nr = fact(n - r); 18 | 19 | int ans = fact_n / (fact_r * fact_nr); 20 | return ans; 21 | } 22 | 23 | 24 | int main() { 25 | 26 | int n = 5, r = 2; 27 | cout << nCr(5, 2) << endl; 28 | 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Lecture-9/ForwardDeclaration: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/ForwardDeclaration -------------------------------------------------------------------------------- /Lecture-9/ForwardDeclaration.cpp: -------------------------------------------------------------------------------- 1 | // ForwardDeclaration.cpp 2 | #include 3 | using namespace std; 4 | 5 | // Forward Declaration 6 | void HelloWorld(); 7 | 8 | 9 | int main() { 10 | 11 | HelloWorld(); 12 | 13 | return 0; 14 | } 15 | 16 | void HelloWorld() { 17 | cout << "Hello World" << endl; 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Lecture-9/Functions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/Functions -------------------------------------------------------------------------------- /Lecture-9/Functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int add(int x, int y) { 5 | return x + y; 6 | } 7 | 8 | int main() { 9 | 10 | int a = 10, b = 20; 11 | cout << add(a, b) << endl; 12 | 13 | return 0; 14 | } 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Lecture-9/MergeSorted: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/MergeSorted -------------------------------------------------------------------------------- /Lecture-9/MergeSorted.cpp: -------------------------------------------------------------------------------- 1 | // MergeSorted.cpp 2 | #include 3 | using namespace std; 4 | 5 | void MergeSorted(int a[], int b[], int n, int m) { 6 | int i = n - 1, j = m - 1, k = m + n - 1; 7 | while (i >= 0 and j >= 0) { 8 | if (a[i] > b[j]) { 9 | a[k] = a[i]; 10 | k--; 11 | i--; 12 | } 13 | else { 14 | a[k--] = b[j--]; 15 | } 16 | } 17 | 18 | while (j >= 0) { 19 | a[k--] = b[j--]; 20 | } 21 | } 22 | 23 | void printArray(int a[], int n) { 24 | for (int i = 0; i < n; ++i) 25 | { 26 | cout << a[i] << " "; 27 | } 28 | cout << endl; 29 | } 30 | 31 | int main() { 32 | 33 | int a[8] = {2, 3, 4, 6}; 34 | int b[4] = {1, 5, 7, 8}; 35 | 36 | int n = 4, m = 4; 37 | 38 | printArray(a, n); 39 | MergeSorted(a, b, n, m); 40 | printArray(a, n + m); 41 | 42 | 43 | return 0; 44 | } 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /Lecture-9/PrintAllPrimes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/PrintAllPrimes -------------------------------------------------------------------------------- /Lecture-9/PrintAllPrimes.cpp: -------------------------------------------------------------------------------- 1 | // PrintAllPrimes.cpp 2 | #include 3 | using namespace std; 4 | // void PrintAllPrimes(int n); 5 | // void PrintAllPrimes(int); 6 | // bool CheckPrime(int n); 7 | // bool CheckPrime(int); 8 | 9 | bool CheckPrime(int n) { 10 | for (int i = 2; i <= n - 1 ; i++) { 11 | if (n % i == 0) { 12 | return false; 13 | } 14 | } 15 | return true; 16 | } 17 | 18 | void PrintAllPrimes(int n) { 19 | for (int no = 2; no <= n; ++no) 20 | { 21 | if (CheckPrime(no) == true) { 22 | cout << no << " "; 23 | } 24 | } 25 | cout << endl; 26 | } 27 | 28 | int main() { 29 | 30 | int n; 31 | cin >> n; 32 | 33 | PrintAllPrimes(n); 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Lecture-9/SelectionSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/19JuneCPP2022/5d031f518f2793fb48f20768a515066102ad7d91/Lecture-9/SelectionSort -------------------------------------------------------------------------------- /Lecture-9/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | // SelectionSort.cpp 2 | #include 3 | using namespace std; 4 | 5 | void SelectionSort(int a[], int n) { 6 | for (int i = 0; i < n - 1; ++i) 7 | { 8 | int mi = i; 9 | for (int j = i + 1; j < n; ++j) 10 | { 11 | if (a[j] < a[mi]) { 12 | mi = j; 13 | } 14 | } 15 | int temp = a[i]; 16 | a[i] = a[mi]; 17 | a[mi] = temp; 18 | } 19 | } 20 | 21 | void printArray(int a[], int n) { 22 | cout << "Printing Array: "; 23 | for (int i = 0; i < n; ++i) 24 | { 25 | cout << a[i] << " "; 26 | } 27 | cout << endl; 28 | } 29 | 30 | 31 | int main() { 32 | 33 | int a[] = {5, 6, 4, 3, 7, 8, 9, 0, 1}; 34 | int n = sizeof(a) / sizeof(int); 35 | 36 | printArray(a, n); 37 | SelectionSort(a, n); 38 | printArray(a, n); 39 | 40 | return 0; 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Lecture-9/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /QPs-DP/Coins.cpp: -------------------------------------------------------------------------------- 1 | // Coins.cpp 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | double solve(int head, int n, vector &p, 8 | vector< vector >& dp) { 9 | if (head == 0) return dp[head][n] = 1; 10 | if (n == 0) return dp[head][n] = 0; // head != 0 11 | if (dp[head][n] > -0.9) return dp[head][n]; 12 | 13 | return dp[head][n] = p[n - 1] * solve(head - 1, n - 1, p, dp) 14 | + (1 - p[n - 1]) * solve(head, n - 1, p, dp); 15 | } 16 | 17 | int main() { 18 | int n; 19 | cin >> n; 20 | vector p(n); 21 | vector< vector > dp(3000, vector(3000, -1)); 22 | 23 | for (int i = 0; i < n; ++i) 24 | { 25 | cin >> p[i]; 26 | } 27 | 28 | cout << setprecision(10) << solve((n + 1) / 2, n, p, dp); 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /QPs-DP/PallindromePartition-2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPallindrome(string &x, int i, int j) { 4 | while (i < j) { 5 | if (x[i++] != x[j--]) return false; 6 | } 7 | return true; 8 | } 9 | int solve(string &s, int start, int end, vector &dp) { 10 | // base case 11 | if (start >= end) { 12 | return dp[start] = 0; 13 | } 14 | if (dp[start] != -1) { 15 | return dp[start]; 16 | } 17 | if (isPallindrome(s, start, end)) { 18 | return dp[start] = 0; 19 | } 20 | // recursive case 21 | int ans = INT_MAX; 22 | for (int i = start; i <= end; ++i) 23 | { 24 | if (isPallindrome(s, start, i)) { 25 | int choteKeCount = solve(s, i + 1, end, dp); 26 | ans = min(ans, 1 + choteKeCount); 27 | } 28 | } 29 | return dp[start] = ans; 30 | } 31 | 32 | int minCut(string s) { 33 | vector dp(s.length() + 1, -1); 34 | 35 | return solve(s, 0, s.length() - 1, dp); 36 | } 37 | }; 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /QPs-DP/Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | return 0; 8 | } 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 19JuneCPP2022 2 | --------------------------------------------------------------------------------