├── .DS_Store ├── L-6-PrefixSum-Subarrays ├── 1_PigeonHole ├── 1_PigeonHole.cpp ├── 2_CompositeColouring ├── 2_CompositeColouring.cpp ├── 3_Prime ├── 3_Prime.cpp ├── 4_PrimeSieve ├── 4_PrimeSieve.cpp ├── 5_PrimeSieveOptimised ├── 5_PrimeSieveOptimised.cpp ├── 6_BearAndPrimes └── 6_BearAndPrimes.cpp ├── L1-Arrays ├── .DS_Store ├── 1_ArrayBasics └── 1_ArrayBasics.cpp ├── L10-Recursion ├── 1_MultiplyTwoNumbers ├── 1_MultiplyTwoNumbers.cpp ├── 2_BubbleSort ├── 2_BubbleSort.cpp ├── 3_BinarySearch ├── 3_BinarySearch.cpp ├── 4_MergeSort ├── 4_MergeSort.cpp ├── 5_Toh ├── 5_Toh.cpp ├── 6_Permutations ├── 6_Permutations.cpp ├── 7_SortedPermutations ├── 7_SortedPermutations.cpp ├── 8_Subsets ├── 8_Subsets.cpp ├── 8_Substring ├── 8_Substring.cpp ├── http_server ├── http_server.c └── server.c ├── L11-Recursion ├── 10_RatInMazeAdvanced ├── 10_RatInMazeAdvanced.cpp ├── 11_NQueen ├── 11_NQueen.cpp ├── 1_Vector ├── 1_Vector.cpp ├── 2_VectorLikeArray ├── 2_VectorLikeArray.cpp ├── 3_2DVector ├── 3_2DVector.cpp ├── 4_2DVector ├── 4_2DVector.cpp ├── 5_Subsets2.cpp ├── 6_CombinationSum.cpp ├── 7_CombinationSum2.cpp ├── 8_PermutationUnique.cpp ├── 9_RatInMaze └── 9_RatInMaze.cpp ├── L12-Recursion ├── 1_SudokuSolver ├── 1_SudokuSolver.cpp ├── 2_PhoneKeypad ├── 2_PhoneKeypad.cpp ├── 3_NStairs ├── 3_NStairs.cpp ├── 4_CountWays ├── 4_CountWays.cpp ├── 5_ElephantWays ├── 5_ElephantWays.cpp ├── 6_BalancedParanthesis ├── 6_BalancedParanthesis.cpp ├── 7_PalindromePartitioning └── 7_PalindromePartitioning.cpp ├── L13-LinkedList ├── 1_Basics ├── 1_Basics.cpp ├── 2_CreateLL ├── 2_CreateLL.cpp ├── 2_DeletionInLL ├── 2_DeletionInLL.cpp ├── 3_reverseLL ├── 3_reverseLL.cpp ├── 4_mergeSortedList ├── 4_mergeSortedList.cpp ├── 5_FindingMiddle ├── 5_FindingMiddle.cpp ├── 6_MergeSort ├── 6_MergeSort.cpp ├── 7_FindKthNodeFromLast ├── 7_FindKthNodeFromLast.cpp ├── 8_CycleDetection └── 8_CycleDetection.cpp ├── L14-StackAndQueue-Discussion ├── 1_Linkedlist-K-Reverse.cpp ├── 1_Pattern-PenPaperRound ├── 1_Pattern-PenPaperRound.cpp ├── 2_OddEvenLL.cpp ├── 2_Pattern-PenPaper-SalescodeAI ├── 2_Pattern-PenPaper-SalescodeAI.cpp ├── 3_OddEvenIndex.cpp ├── 4_RemoveDuplicates.cpp ├── 5_Stack ├── 5_Stack.cpp ├── 6_StackVector ├── 6_StackVector.cpp ├── 7_StackUsingLL └── 7_StackUsingLL.cpp ├── L16-Trees ├── 10_ZigZagTraversalPrint ├── 10_ZigZagTraversalPrint.cpp ├── 1_Test ├── 1_Test.cpp ├── 2_CountNodes ├── 2_CountNodes.cpp ├── 3_HeightOfTree ├── 3_HeightOfTree.cpp ├── 4_Diameter ├── 4_Diameter.cpp ├── 5_FindNode ├── 5_FindNode.cpp ├── 6_MirrorBT ├── 6_MirrorBT.cpp ├── 7_Levelorder ├── 7_Levelorder.cpp ├── 8_InputTreeLevelWise ├── 8_InputTreeLevelWise.cpp ├── 9_RightViewOfTree ├── 9_RightViewOfTree.cpp └── input.txt ├── L17-Trees ├── 1_LCA ├── 1_LCA.cpp ├── 2_NodeAtK ├── 2_NodeAtK.cpp ├── 3_HouseRobber3.cpp ├── 4_BinaryTreeCamera.cpp └── input.txt ├── L18-BST ├── 1_BST ├── 1_BST.cpp ├── 2_PrintRangeElements ├── 2_PrintRangeElements.cpp ├── 3_SearchBST ├── 3_SearchBST.cpp ├── 4_isBST ├── 4_isBST.cpp ├── 5_isBSTFast ├── 5_isBSTFast.cpp ├── CBNumber.cpp ├── input.txt └── random.java ├── L19-TreesAndHeap ├── 1_BSTtoLL ├── 1_BSTtoLL.cpp ├── 2_kthSmallestElement.cpp ├── 3_uniqueBST.cpp ├── 4_NcR ├── 4_NcR.cpp └── input.txt ├── L2-Arrays ├── 1_Basics ├── 1_Basics.cpp ├── 2_Continue ├── 2_Continue.cpp ├── 3_SubArrays ├── 3_SubArrays.cpp ├── 4_PrintSubarrayWithMaxSum ├── 4_PrintSubarrayWithMaxSum.cpp ├── 5_MaxSumTwoArrays ├── 5_MaxSumTwoArrays.cpp ├── 6_Kadanes └── 6_Kadanes.cpp ├── L3-Arrays ├── 1_ProductOfArrayExceptSelf ├── 1_ProductOfArrayExceptSelf.cpp ├── 2_ProductOfArrayBestApproach ├── 2_ProductOfArrayBestApproach.cpp ├── 3_RainWaterTrapping ├── 3_RainWaterTrapping.cpp ├── 4_3Sum ├── 4_3Sum.cpp ├── 5_Sorting ├── 5_Sorting.cpp ├── 6_PatchingArray └── 6_PatchingArray.cpp ├── L4-Arrays ├── 1_DayAtBeach ├── 1_DayAtBeach.cpp ├── 2_BinarySearch ├── 2_BinarySearch.cpp ├── 3_MedianOfSorted ├── 3_MedianOfSorted.cpp ├── 3_SearchRotatedSorted └── 3_SearchRotatedSorted.cpp ├── L5-BinarySearch ├── 1_Test. pp ├── 1_Test.cpp ├── 2_AggressiveCows ├── 2_AggressiveCows.cpp ├── 3_bookAllocation ├── 3_bookAllocation.cpp ├── 4_MurthalParantha └── 4_MurthalParantha.cpp ├── L7-Arrays-2DArrays ├── 1_PashaAndStrings ├── 1_PashaAndStrings.cpp ├── 2_2DArrayBasics ├── 2_2DArrayBasics.cpp ├── 3_Transpose ├── 3_Transpose.cpp ├── 4_WavePrint ├── 4_WavePrint.cpp ├── 5_SpiralPrint ├── 5_SpiralPrint.cpp ├── 6_CharacterArrays ├── 6_CharacterArrays.cpp ├── 7_input ├── 7_input.cpp ├── 8_RotateString ├── 8_RotateString.cpp ├── input.txt └── output.txt ├── L8-CharacterArrays-Recursion ├── 1_RecursionBasics ├── 1_RecursionBasics.cpp ├── 1_Test.cpp ├── 2_ConcatenateStrings ├── 2_ConcatenateStrings.cpp ├── 2_SumOfN ├── 2_SumOfN.cpp ├── 3_Fibo ├── 3_Fibo.cpp ├── 3_LargestString ├── 3_LargestString.cpp ├── 4_NextPermutation ├── 4_NextPermutation.cpp ├── 4_SumOfArray ├── 4_SumOfArray.cpp ├── 5_StringToInt ├── 5_StringToInt.cpp ├── 6_CharToInt ├── 6_CharToInt.cpp ├── input.txt └── output.txt ├── L9-Recursion_ClassTest ├── 1_SortArray ├── 1_SortArray.cpp ├── 2_IntersectionOfArrays.cpp ├── 3_HoodiesAtCB ├── 3_HoodiesAtCB.cpp ├── 4_PlayingCards ├── 4_PlayingCards.cpp ├── 5_First7 ├── 5_First7.cpp ├── 6_Last7 ├── 6_Last7.cpp ├── 7_All7 ├── 7_All7.cpp └── SortArray.cpp ├── Lec-1-Arrays ├── Lecture-15-StackAndQueues ├── 10_OkabeAndBoxes ├── 10_OkabeAndBoxes.cpp ├── 11_Queue ├── 11_Queue.cpp ├── 12_CircularQueue ├── 12_CircularQueue.cpp ├── 132Pattern.cpp ├── 1_Test ├── 1_Test.cpp ├── 2_NextGreater.cpp ├── 3_PreviousGreater ├── 3_PreviousGreater.cpp ├── 4_LargestRectangle.cpp ├── 5_NextSmaller ├── 5_NextSmaller.cpp ├── 6_PreviousSmaller ├── 6_PreviousSmaller.cpp ├── 7_OnlineStockSpan.cpp ├── 9_BalancedParanthesis └── 9_BalancedParanthesis.cpp └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/.DS_Store -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/1_PigeonHole: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L-6-PrefixSum-Subarrays/1_PigeonHole -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/1_PigeonHole.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {1, 2, 3, 4, 5}; 7 | int n = sizeof(a) / sizeof(int); 8 | 9 | int ps[100] = {0}; 10 | // 1. Finding prefixsum array 11 | ps[0] = a[0]; 12 | for (int i = 1; i < n; ++i) 13 | { 14 | ps[i] = ps[i - 1] + a[i]; 15 | } 16 | 17 | // 2. Take mod of ps[] with n 18 | for (int i = 0; i < n; ++i) 19 | { 20 | ps[i] %= n; 21 | } 22 | 23 | // 3. Finding the remainders count 24 | int remaindersCount[100] = {1}; // this is to compensate for the 25 | // empty subarray jiska sum is zero toh usse remaindersCount mei pehle hi lelo 26 | 27 | // [1,0,0,0,0,0,0,0,0 .... 0] 28 | for (int i = 0; i < n; ++i) 29 | { 30 | remaindersCount[ps[i]]++; 31 | } 32 | 33 | // nc2 = n*(n-1)/2 34 | int ans = 0; 35 | for (int i = 0; i < n; ++i) 36 | { 37 | ans += (remaindersCount[i] * (remaindersCount[i] - 1) / 2); 38 | } 39 | 40 | cout << "Total subarrays: " << ans << endl; 41 | 42 | 43 | return 0; 44 | } 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/2_CompositeColouring: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L-6-PrefixSum-Subarrays/2_CompositeColouring -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/2_CompositeColouring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; 5 | 6 | int main() { 7 | 8 | int t; 9 | cin >> t; 10 | 11 | while (t--) { 12 | int n; 13 | cin >> n; 14 | int a[1005]; 15 | // by default sabhi par color 0 kar diya 16 | int rang[1005] = {0}; 17 | 18 | for (int i = 0; i < n; ++i) cin >> a[i]; 19 | 20 | int color = 1; // color hum 1 se fill krna start krenge 21 | 22 | for (int i = 0; i < 11; ++i) 23 | { 24 | int x = prime[i]; 25 | // Jo bhi element colored nhi hai aur multiple hai x ka 26 | // un sab par same color kardo 27 | bool kyaColorFillKia = false; 28 | for (int j = 0; j < n; ++j) 29 | { 30 | if (rang[j] != 0) continue; // ek baari jispar color kar diya 31 | // uspar dobara color nhi krna hai 32 | 33 | if (a[j] % x == 0) { // saare elements jo x se divide hote h 34 | // unpar same color kar skte hai 35 | rang[j] = color; 36 | kyaColorFillKia = true; 37 | } 38 | } 39 | 40 | // Agar loop ke andar currentColor use hua toh aage walo par next 41 | // color use krenge otherwise abhi wala use hi nhi kia toh whi karo 42 | if (kyaColorFillKia) color++; 43 | 44 | } 45 | 46 | cout << color - 1 << endl; 47 | for (int i = 0; i < n; ++i) 48 | { 49 | cout << rang[i] << " "; 50 | } 51 | cout << endl; 52 | 53 | } 54 | 55 | return 0; 56 | } 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/3_Prime: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L-6-PrefixSum-Subarrays/3_Prime -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/3_Prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin >> n; 8 | bool isPrime = true; 9 | for (int i = 2; i < n; ++i) 10 | { 11 | if (n % i == 0) { 12 | isPrime = false; 13 | break; 14 | } 15 | } 16 | 17 | if (isPrime) cout << "Prime\n"; 18 | else cout << "Not Prime\n"; 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/4_PrimeSieve: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L-6-PrefixSum-Subarrays/4_PrimeSieve -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/4_PrimeSieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | bool a[100005]; 7 | for (int i = 0; i < 100005; ++i) 8 | { 9 | a[i] = true; 10 | } 11 | 12 | int n; 13 | cin >> n; 14 | 15 | for (int i = 2; i <= n; ++i) 16 | { 17 | if (a[i] == true) { 18 | for (int j = 2 * i; j <= n; j += i) 19 | { 20 | a[j] = false; 21 | } 22 | } 23 | 24 | } 25 | 26 | for (int i = 2; i <= n; ++i) 27 | { 28 | if (a[i]) cout << i << " "; 29 | } 30 | cout << endl; 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/5_PrimeSieveOptimised: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L-6-PrefixSum-Subarrays/5_PrimeSieveOptimised -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/5_PrimeSieveOptimised.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | 7 | 8 | 9 | for (int i = 2; i <= n; ++i) 10 | { 11 | if (a[i]) cout << i << " "; 12 | } 13 | cout << endl; 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/6_BearAndPrimes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L-6-PrefixSum-Subarrays/6_BearAndPrimes -------------------------------------------------------------------------------- /L-6-PrefixSum-Subarrays/6_BearAndPrimes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int MAX_VAL = 1e7 + 5; 6 | vector primes(MAX_VAL, true); 7 | vector freq(MAX_VAL, 0); 8 | vector ps(MAX_VAL, 0); 9 | 10 | void sieve() { 11 | primes[0] = primes[1] = false; 12 | for (int i = 2; i * i < MAX_VAL; ++i) 13 | { 14 | if (primes[i] == true) { 15 | for (int j = i * i; j < MAX_VAL; j += i) 16 | { 17 | primes[j] = false; 18 | } 19 | } 20 | } 21 | } 22 | 23 | int main() { 24 | 25 | sieve(); 26 | 27 | int n; 28 | cin >> n; 29 | for (int i = 0; i < n; ++i) 30 | { 31 | int x; cin >> x; 32 | freq[x]++; 33 | } 34 | 35 | // prime numbers se array ke kitne elements divide hote hai uska count 36 | for (int i = 2; i < MAX_VAL; ++i) 37 | { 38 | if (primes[i]) { 39 | for (int j = i; j < MAX_VAL; j += i) 40 | { 41 | ps[i] += freq[j]; 42 | } 43 | } 44 | } 45 | 46 | // Actually mei find prefix sum 47 | for (int i = 2; i < MAX_VAL; ++i) 48 | { 49 | ps[i] = ps[i] + ps[i - 1]; 50 | } 51 | 52 | int m; 53 | cin >> m; 54 | 55 | for (int i = 0; i < m; ++i) 56 | { 57 | int l, r; 58 | cin >> l >> r; 59 | 60 | l = min(l, MAX_VAL - 1); 61 | r = min(r, MAX_VAL - 1); 62 | 63 | cout << ps[r] - ps[l - 1] << endl; 64 | } 65 | 66 | return 0; 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /L1-Arrays/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L1-Arrays/.DS_Store -------------------------------------------------------------------------------- /L1-Arrays/1_ArrayBasics: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L1-Arrays/1_ArrayBasics -------------------------------------------------------------------------------- /L1-Arrays/1_ArrayBasics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {1, 2, 3, 4, 5}; // Initialisation: Array ko banante time value dena 7 | // int a[10] = {1, 2, 3, 4, 5}; // Initialisation: Array ko banante time value dena 8 | int n = sizeof(a) / sizeof(int); 9 | 10 | for (int i = 0; i < n; ++i) 11 | { 12 | cout << a[i] << " "; 13 | } 14 | 15 | cout << endl; 16 | 17 | return 0; 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /L10-Recursion/1_MultiplyTwoNumbers: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/1_MultiplyTwoNumbers -------------------------------------------------------------------------------- /L10-Recursion/1_MultiplyTwoNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int a, int b) { 5 | // base case 6 | if (b == 0) return 0; 7 | 8 | // recursive case 9 | return a + solve(a, b - 1); 10 | } 11 | 12 | int main() { 13 | 14 | cout << solve(5, 3) << endl; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /L10-Recursion/2_BubbleSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/2_BubbleSort -------------------------------------------------------------------------------- /L10-Recursion/2_BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int *a, int n) { 5 | 6 | for (int i = 0; i < n - 1; ++i) 7 | { 8 | for (int j = 0; j < n - 1 - i; ++j) 9 | { 10 | if (a[j] > a[j + 1]) { 11 | swap(a[j], a[j + 1]); 12 | } 13 | } 14 | } 15 | } 16 | 17 | void solve(int *a, int n, int i) { 18 | // base case 19 | if (i == n - 1) return; 20 | 21 | // recursive case 22 | // Pehle number ko sahi jagah rakhdo, baaki ka kaam recursion ko boldenge 23 | for (int j = 0; j < n - 1 - i; ++j) 24 | { 25 | if (a[j] > a[j + 1]) { 26 | swap(a[j], a[j + 1]); 27 | } 28 | } 29 | 30 | // Ab baaki ke numbers ko sahi jagah recursion se rakhwa do 31 | solve(a, n, i + 1); // Assumption hai ki recursion kaam kr dega 32 | } 33 | 34 | void solve1(int *a, int n, int i, int j) { 35 | // base case 36 | if (i == n - 1) return; 37 | 38 | // recursive case 39 | 40 | // Ab baaki ke numbers ko sahi jagah recursion se rakhwa do 41 | if (j == n - 1 - i) 42 | solve1(a, n, i + 1, 0); // Assumption hai ki recursion kaam kr dega 43 | else { 44 | if (a[j] > a[j + 1]) { 45 | swap(a[j], a[j + 1]); 46 | } 47 | 48 | solve1(a, n, i, j + 1); // J ka loop chal raha hai 49 | } 50 | } 51 | 52 | int main() { 53 | 54 | int a[] = {5, 4, 3, 2, 1}; 55 | int n = sizeof(a) / sizeof(int); 56 | 57 | solve1(a, n, 0, 0); 58 | 59 | for (int i = 0; i < n; ++i) 60 | { 61 | cout << a[i] << " "; 62 | } 63 | 64 | 65 | 66 | return 0; 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /L10-Recursion/3_BinarySearch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/3_BinarySearch -------------------------------------------------------------------------------- /L10-Recursion/3_BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(int *a, int s, int e, int key) { 5 | // base case 6 | if (s > e) return -1; 7 | 8 | // recursive case 9 | int mid = (s + e) / 2; 10 | if (a[mid] == key) return mid; 11 | else if (key > a[mid]) { 12 | int indx = binarySearch(a, mid + 1, e, key); 13 | return indx; 14 | } 15 | else { 16 | return binarySearch(a, s, mid - 1, key); 17 | } 18 | } 19 | 20 | int main() { 21 | 22 | int a[] = {1, 2, 3, 4, 5}; 23 | int n = sizeof(a) / sizeof(int); 24 | int key = 10; 25 | 26 | cout << binarySearch(a, 0, n - 1, key) << endl; 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /L10-Recursion/4_MergeSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/4_MergeSort -------------------------------------------------------------------------------- /L10-Recursion/4_MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void mergeArrays(int *a, int *b, int *c, int s, int e) { 5 | int mid = (s + e) / 2; 6 | 7 | int i = s, j = s, k = mid + 1; 8 | while (j <= mid and k <= e) { 9 | if (b[j] < c[k]) a[i++] = b[j++]; 10 | else a[i++] = c[k++]; 11 | } 12 | 13 | while (j <= mid) a[i++] = b[j++]; 14 | while (k <= e) a[i++] = c[k++]; 15 | } 16 | 17 | void mergeSort(int *a, int s, int e) { 18 | // base case 19 | if (s == e) return; 20 | 21 | // recursive 22 | // 1. Divide 23 | int b[100005], c[100005]; 24 | int mid = (s + e) / 2; 25 | // Copy s,mid in array b[] 26 | for (int i = s; i <= mid; ++i) 27 | { 28 | b[i] = a[i]; 29 | } 30 | 31 | for (int i = mid + 1; i <= e; ++i) 32 | { 33 | c[i] = a[i]; 34 | } 35 | 36 | // 2. Sort 37 | mergeSort(b, s, mid); 38 | mergeSort(c, mid + 1, e); 39 | // 3. Merge 40 | mergeArrays(a, b, c, s, e); 41 | } 42 | 43 | int main() { 44 | int a[] = {6, 5, 4, 3, 2, 1}; 45 | int n = sizeof(a) / sizeof(int); 46 | 47 | mergeSort(a, 0, n - 1); 48 | for (int i = 0; i < n; ++i) 49 | { 50 | cout << a[i] << " "; 51 | } 52 | cout << endl; 53 | 54 | 55 | return 0; 56 | } 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /L10-Recursion/5_Toh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/5_Toh -------------------------------------------------------------------------------- /L10-Recursion/5_Toh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void toh(int n, char src, char helper, char des) { 5 | // base case 6 | if (n == 0) return; 7 | 8 | // recursive case 9 | toh(n - 1, src, des, helper); 10 | cout << n << " -> " << src << " to " << des << endl; 11 | toh(n - 1, helper, src, des); 12 | } 13 | 14 | int main() { 15 | 16 | int n = 3; 17 | toh(n, 'A', 'B', 'C'); 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /L10-Recursion/6_Permutations: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/6_Permutations -------------------------------------------------------------------------------- /L10-Recursion/6_Permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector v; 7 | void permutations(char *a, int i) { 8 | // base case 9 | if (a[i] == '\0') { 10 | // cout << a << endl; 11 | string x(a); 12 | v.push_back(x); 13 | return; 14 | } 15 | // recursive case 16 | for (int j = i; a[j] != '\0'; ++j) 17 | { 18 | swap(a[i], a[j]); 19 | permutations(a, i + 1); 20 | swap(a[i], a[j]); // Backtracking 21 | } 22 | } 23 | 24 | int main() { 25 | 26 | char a[] = "abc"; 27 | permutations(a, 0); 28 | sort(v.begin(), v.end()); 29 | 30 | for (int i = 0; i < v.size(); ++i) 31 | { 32 | cout << v[i] << " "; 33 | } 34 | cout << endl; 35 | 36 | return 0; 37 | } 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /L10-Recursion/7_SortedPermutations: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/7_SortedPermutations -------------------------------------------------------------------------------- /L10-Recursion/7_SortedPermutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void permutations(string ip, string op) { 5 | // base case 6 | if (ip.size() == 0) { 7 | cout << op << " "; 8 | return; 9 | } 10 | // recursive case 11 | for (int i = 0; i < ip.size(); ++i) 12 | { 13 | char ch = ip[i]; 14 | permutations(ip.substr(0, i) + ip.substr(i + 1) , op + ip[i]); 15 | } 16 | } 17 | 18 | int main() { 19 | string s = "bac"; 20 | sort(s.begin(), s.end()); 21 | // int indx = 2; 22 | // cout << s.substr(0, indx) << endl; 23 | // cout << s.substr(indx + 1) << endl; 24 | 25 | 26 | permutations(s, ""); 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /L10-Recursion/8_Subsets: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/8_Subsets -------------------------------------------------------------------------------- /L10-Recursion/8_Subsets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve(int *a, int i, int n, int *b, int j) { 5 | // base case 6 | if (i == n) { 7 | // print kardo b[] ko, that will include j elements in it 8 | for (int k = 0; k < j; ++k) 9 | { 10 | cout << b[k] << " "; 11 | } 12 | cout << endl; 13 | return; 14 | } 15 | // recursive case 16 | // 1. ith ko mat lo b mei 17 | solve(a, i + 1, n, b, j); 18 | // 2. ith ko lelo b[] mei 19 | b[j] = a[i]; 20 | solve(a, i + 1, n, b, j + 1); 21 | } 22 | 23 | int main() { 24 | 25 | int a[] = {1, 2, 3}; 26 | int n = sizeof(a) / sizeof(int); 27 | int b[200]; 28 | solve(a, 0, n, b, 0); 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /L10-Recursion/8_Substring: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/8_Substring -------------------------------------------------------------------------------- /L10-Recursion/8_Substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | string x = "abcde"; 7 | int i = 4; 8 | string n = x.substr(0, i) + x.substr(i + 1); 9 | cout << n << endl; 10 | return 0; 11 | } 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /L10-Recursion/http_server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L10-Recursion/http_server -------------------------------------------------------------------------------- /L10-Recursion/http_server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define PORT 8080 9 | #define BUFFER_SIZE 1024 10 | 11 | int main() { 12 | int server_fd, new_socket; 13 | struct sockaddr_in address; 14 | int addrlen = sizeof(address); 15 | char buffer[BUFFER_SIZE] = {0}; 16 | char *http_response = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nHello world!"; 17 | 18 | // Creating socket file descriptor 19 | if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { 20 | perror("socket failed"); 21 | exit(EXIT_FAILURE); 22 | } 23 | 24 | // Forcefully attaching socket to the port 8080 25 | if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &(int) { 1 }, sizeof(int)) < 0) { 26 | perror("setsockopt"); 27 | exit(EXIT_FAILURE); 28 | } 29 | address.sin_family = AF_INET; 30 | address.sin_addr.s_addr = INADDR_ANY; 31 | address.sin_port = htons(PORT); 32 | 33 | // Bind the socket to the address 34 | if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { 35 | perror("bind failed"); 36 | exit(EXIT_FAILURE); 37 | } 38 | 39 | // Listen to incoming connections 40 | if (listen(server_fd, 3) < 0) { 41 | perror("listen"); 42 | exit(EXIT_FAILURE); 43 | } 44 | 45 | // Accept an incoming connection 46 | if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { 47 | perror("accept"); 48 | exit(EXIT_FAILURE); 49 | } 50 | 51 | // Read the incoming request 52 | read(new_socket, buffer, BUFFER_SIZE); 53 | printf("HTTP Request:\n%s\n", buffer); 54 | 55 | // Send the HTTP response 56 | send(new_socket, http_response, strlen(http_response), 0); 57 | printf("HTTP Response sent\n"); 58 | 59 | // Close the socket 60 | close(new_socket); 61 | close(server_fd); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /L10-Recursion/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define PORT 8080 9 | #define BUFFER_SIZE 1024 10 | 11 | int main() { 12 | int server_fd, new_socket; 13 | struct sockaddr_in address; 14 | int opt = 1; 15 | int addrlen = sizeof(address); 16 | char buffer[BUFFER_SIZE] = {0}; 17 | char *http_response = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 12\n\nHello world!"; 18 | 19 | // Creating socket file descriptor 20 | if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { 21 | perror("socket failed"); 22 | exit(EXIT_FAILURE); 23 | } 24 | 25 | // Forcefully attaching socket to the port 8080 26 | if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { 27 | perror("setsockopt"); 28 | exit(EXIT_FAILURE); 29 | } 30 | address.sin_family = AF_INET; 31 | address.sin_addr.s_addr = INADDR_ANY; 32 | address.sin_port = htons(PORT); 33 | 34 | // Bind the socket to the address 35 | if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { 36 | perror("bind failed"); 37 | exit(EXIT_FAILURE); 38 | } 39 | 40 | // Listen to incoming connections 41 | if (listen(server_fd, 3) < 0) { 42 | perror("listen"); 43 | exit(EXIT_FAILURE); 44 | } 45 | 46 | // Accept an incoming connection 47 | if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { 48 | perror("accept"); 49 | exit(EXIT_FAILURE); 50 | } 51 | 52 | // Read the incoming request 53 | read(new_socket, buffer, BUFFER_SIZE); 54 | printf("HTTP Request:\n%s\n", buffer); 55 | 56 | // Send the HTTP response 57 | send(new_socket, http_response, strlen(http_response), 0); 58 | printf("HTTP Response sent\n"); 59 | 60 | // Close the socket 61 | close(new_socket); 62 | close(server_fd); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /L11-Recursion/10_RatInMazeAdvanced: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L11-Recursion/10_RatInMazeAdvanced -------------------------------------------------------------------------------- /L11-Recursion/10_RatInMazeAdvanced.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | bool ratInMaze(char maze[][6], int i, int j, int sol[][100], int n, int m) { 6 | // base case 7 | if (i == n - 1 and j == m - 1) { 8 | sol[i][j] = 1; 9 | for (int i = 0; i < n; ++i) 10 | { 11 | for (int j = 0; j < m; ++j) 12 | { 13 | cout << sol[i][j] << " "; 14 | } 15 | cout << endl; 16 | } 17 | cout << endl; 18 | 19 | sol[i][j] = 0; // backtracking for final destination cell 20 | return false; 21 | } 22 | 23 | // recursive case 24 | // Jis i,j par khade hai usse abhi solution ka part maan lo 25 | sol[i][j] = 1; 26 | 27 | // Choti maze ka kaam recursion se bolna h 28 | // Check right 29 | if (j + 1 < m and maze[i][j + 1] != 'X' and sol[i][j + 1] == 0) { 30 | bool kyaRightSeBaatBani = ratInMaze(maze, i, j + 1, sol, n, m); 31 | if (kyaRightSeBaatBani) return true; 32 | } 33 | 34 | // Check down 35 | if (i + 1 < n and maze[i + 1][j] != 'X' and sol[i + 1][j] == 0) { 36 | bool kyaDownSeBaatBani = ratInMaze(maze, i + 1, j, sol, n, m); 37 | if (kyaDownSeBaatBani) return true; 38 | } 39 | 40 | // Check up 41 | if (i - 1 >= 0 and maze[i - 1][j] != 'X' and sol[i - 1][j] == 0) { 42 | bool kyaUparSeBaatBani = ratInMaze(maze, i - 1, j, sol, n, m); 43 | if (kyaUparSeBaatBani) return true; 44 | } 45 | 46 | // Check backwards 47 | if (j - 1 >= 0 and maze[i][j - 1] != 'X' and sol[i][j - 1] == 0) { 48 | bool kyaPeecheSeBaatBani = ratInMaze(maze, i, j - 1, sol, n, m); 49 | if (kyaPeecheSeBaatBani) return true; 50 | } 51 | 52 | // Na toh right se raasta milla, na down se 53 | sol[i][j] = 0; // Backtracking 54 | return false; 55 | } 56 | 57 | int main() { 58 | char maze[][6] = { 59 | "0X000", 60 | "0X0X0", 61 | "0X0X0", 62 | "000X0", 63 | }; 64 | 65 | int sol[100][100] = {0}; 66 | 67 | ratInMaze(maze, 0, 0, sol, 4, 5); 68 | 69 | return 0; 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /L11-Recursion/11_NQueen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L11-Recursion/11_NQueen -------------------------------------------------------------------------------- /L11-Recursion/11_NQueen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool safeHai(int board[][100], int i, int j, int n) { 5 | int oi = i, oj = j; 6 | // Left diagonal 7 | while (i >= 0 and j >= 0) { 8 | if (board[i][j] == 1) { // Queen hai already is diagonal pr 9 | return false; 10 | } 11 | i--; 12 | j--; 13 | } 14 | 15 | // Right diagonal 16 | i = oi; j = oj; // wapis reset kara i and j ko unki original pos par 17 | while (i >= 0 and j < n) { 18 | if (board[i][j] == 1) { // Queen hai already is diagonal pr 19 | return false; 20 | } 21 | i--; 22 | j++; 23 | } 24 | 25 | // Columns 26 | i = oi; 27 | j = oj; 28 | for (int k = 0; k < i; ++k) 29 | { 30 | if (board[k][j] == 1) { // Queen hai already is column pr 31 | return false; 32 | } 33 | } 34 | 35 | return true; // Agar upar kahi bhi queen nhi hai toh safe hai 36 | } 37 | 38 | bool nQueen(int board[][100], int n, int i) { 39 | // base case 40 | if (i == n) { 41 | // We have placed n queens on board 42 | // print the board 43 | for (int i = 0; i < n; ++i) 44 | { 45 | for (int j = 0; j < n; ++j) 46 | { 47 | cout << board[i][j] << " "; 48 | } 49 | cout << endl; 50 | } 51 | cout << endl; 52 | 53 | return false; 54 | } 55 | // recursive case 56 | for (int j = 0; j < n; ++j) 57 | { 58 | if (safeHai(board, i, j, n)) { 59 | board[i][j] = 1; 60 | bool kyaBakiQueenPlaceHui = nQueen(board, n, i + 1); 61 | if (kyaBakiQueenPlaceHui) return true; 62 | 63 | board[i][j] = 0; // Backtracking 64 | } 65 | } 66 | 67 | return false; 68 | } 69 | 70 | int main() { 71 | 72 | int board[100][100] = {0}; 73 | int n = 4; 74 | 75 | nQueen(board, n, 0); 76 | 77 | return 0; 78 | } 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /L11-Recursion/1_Vector: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L11-Recursion/1_Vector -------------------------------------------------------------------------------- /L11-Recursion/1_Vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | vector v; 8 | // v.push_back(1); 9 | // v.push_back(2); 10 | // v.push_back(3); 11 | // v.push_back(4); 12 | // v.push_back(5); 13 | 14 | for (int i = 1; i <= 5; ++i) 15 | { 16 | v.push_back(i); 17 | cout << "After insertion " << i << ", size: " << v.size() << ", cap: " << v.capacity() << endl; 18 | } 19 | 20 | for (int i = 0; i < v.size(); ++i) 21 | { 22 | cout << v[i] << " "; 23 | } 24 | cout << endl; 25 | 26 | 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /L11-Recursion/2_VectorLikeArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L11-Recursion/2_VectorLikeArray -------------------------------------------------------------------------------- /L11-Recursion/2_VectorLikeArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | // int a[100]; 7 | int n; 8 | cin >> n; 9 | // Run time memory allocation 10 | // vector a(n); // Number of buckets that i need inside vector 11 | // or we can do this 12 | vector a; 13 | a.reserve(n); // n buckets create kr dega 14 | 15 | for (int i = 0; i < n; ++i) 16 | { 17 | a[i] = i + 1; 18 | } 19 | 20 | for (int i = 0; i < n; ++i) 21 | { 22 | cout << a[i] << " "; 23 | } 24 | cout << endl; 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /L11-Recursion/3_2DVector: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L11-Recursion/3_2DVector -------------------------------------------------------------------------------- /L11-Recursion/3_2DVector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n = 4, m = 5; 7 | vector > v(n, vector(m)); 8 | int num = 1; 9 | 10 | for (int i = 0; i < n; ++i) 11 | { 12 | for (int j = 0; j < m; ++j) 13 | { 14 | v[i][j] = num++; 15 | } 16 | } 17 | 18 | for (int i = 0; i < n; ++i) 19 | { 20 | for (int j = 0; j < m; ++j) 21 | { 22 | cout << v[i][j] << " "; 23 | } 24 | cout << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /L11-Recursion/4_2DVector: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L11-Recursion/4_2DVector -------------------------------------------------------------------------------- /L11-Recursion/4_2DVector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n = 3, m = 3; 7 | vector< vector > v; 8 | 9 | // Initialisation of vectors 10 | vector v1 = {1, 2, 3}; 11 | vector v2 = {11, 12, 13}; 12 | vector v3 = {21, 22, 23}; 13 | 14 | v.push_back(v1); 15 | v.push_back(v2); 16 | v.push_back(v3); 17 | 18 | for (int i = 0; i < n; ++i) 19 | { 20 | for (int j = 0; j < m; ++j) 21 | { 22 | cout << v[i][j] << " "; 23 | } 24 | cout << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /L11-Recursion/5_Subsets2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector > ans; 4 | 5 | void solve(vector &ip, int i, vector &op) { 6 | // All the op[] are answer 7 | ans.push_back(op); 8 | 9 | // base case 10 | if (i == ip.size()) return; 11 | // recursive case 12 | for (int j = i; j < ip.size(); ++j) 13 | { 14 | if (j != i and ip[j] == ip[j - 1]) continue; 15 | 16 | op.push_back(ip[j]); 17 | solve(ip, j + 1, op); 18 | op.pop_back(); // backtracking 19 | } 20 | } 21 | 22 | vector> subsetsWithDup(vector& ip) { 23 | vector op; 24 | sort(ip.begin(), ip.end()); 25 | solve(ip, 0, op); 26 | return ans; 27 | } 28 | }; 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /L11-Recursion/6_CombinationSum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector > ans; 4 | 5 | void solve(vector& ip, int target, vector &op, int i = 0) { 6 | if (target < 0) return; 7 | // base case 8 | if (target == 0) { 9 | ans.push_back(op); 10 | return; 11 | } 12 | // recursive case 13 | for (int j = i ; j < ip.size(); j++) { 14 | op.push_back(ip[j]); 15 | solve(ip, target - ip[j], op, j); 16 | op.pop_back(); 17 | } 18 | } 19 | 20 | vector> combinationSum(vector& ip, int target) { 21 | vector op; 22 | solve(ip, target, op); 23 | return ans; 24 | } 25 | }; -------------------------------------------------------------------------------- /L11-Recursion/7_CombinationSum2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector > ans; 4 | 5 | void solve(vector& ip, int target, vector &op, int i = 0) { 6 | if (target < 0) return; 7 | // base case 8 | if (target == 0) { 9 | ans.push_back(op); 10 | return; 11 | } 12 | // recursive case 13 | for (int j = i ; j < ip.size(); j++) { 14 | if (j != i and ip[j] == ip[j - 1]) continue; 15 | 16 | op.push_back(ip[j]); 17 | solve(ip, target - ip[j], op, j + 1); 18 | op.pop_back(); 19 | } 20 | } 21 | vector> combinationSum2(vector& ip, int target) { 22 | sort(ip.begin(), ip.end()); 23 | vector op; 24 | solve(ip, target, op); 25 | return ans; 26 | } 27 | }; 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /L11-Recursion/8_PermutationUnique.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector > x; 4 | void solve(vector& a, int i = 0) { 5 | // base case 6 | if (i == a.size()) { 7 | 8 | x.push_back(a); 9 | return; 10 | } 11 | 12 | // recursive case 13 | int freq[25] = {0}; 14 | for (int j = i; j < a.size(); j++) { 15 | // We have to prevent a digit to go at ith position once we have 16 | // already considered it.. 17 | if (freq[a[j] + 10] != 0) continue; 18 | freq[a[j] + 10] = 1; 19 | swap(a[i], a[j]); 20 | solve(a, i + 1); 21 | swap(a[i], a[j]); 22 | } 23 | } 24 | 25 | vector> permuteUnique(vector& nums) { 26 | // sort(nums.begin(), nums.end()); 27 | solve(nums); 28 | return x; 29 | } 30 | }; 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /L11-Recursion/9_RatInMaze: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L11-Recursion/9_RatInMaze -------------------------------------------------------------------------------- /L11-Recursion/9_RatInMaze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool ratInMaze(char maze[][5], int i, int j, int sol[][100], int n, int m) { 5 | // base case 6 | if (i == n - 1 and j == m - 1) { 7 | sol[i][j] = 1; // n-1 and m-1 bhi solution ka part honge 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 | 18 | return false; 19 | } 20 | 21 | // recursive case 22 | // Jis i,j par khade hai usse abhi solution ka part maan lo 23 | sol[i][j] = 1; 24 | 25 | // Choti maze ka kaam recursion se bolna h 26 | // Check right 27 | if (j + 1 < m and maze[i][j + 1] != 'X') { 28 | bool kyaRightSeBaatBani = ratInMaze(maze, i, j + 1, sol, n, m); 29 | if (kyaRightSeBaatBani) return true; 30 | } 31 | 32 | // Check down 33 | if (i + 1 < n and maze[i + 1][j] != 'X') { 34 | bool kyaDownSeBaatBani = ratInMaze(maze, i + 1, j, sol, n, m); 35 | if (kyaDownSeBaatBani) return true; 36 | } 37 | 38 | // Na toh right se raasta milla, na down se 39 | sol[i][j] = 0; // Backtracking 40 | return false; 41 | } 42 | 43 | int main() { 44 | 45 | char maze[][5] = { 46 | "0000", 47 | "00XX", 48 | "0000", 49 | "XX00", 50 | }; 51 | 52 | int sol[100][100] = {0}; 53 | 54 | ratInMaze(maze, 0, 0, sol, 4, 4); 55 | 56 | return 0; 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /L12-Recursion/1_SudokuSolver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L12-Recursion/1_SudokuSolver -------------------------------------------------------------------------------- /L12-Recursion/1_SudokuSolver.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isSafe(int board[][9], int i, int j, int n, int no) { 6 | // Lets check row and column 7 | for (int k = 0; k < n; ++k) 8 | { 9 | if (board[i][k] == no || board[k][j] == no) return false; 10 | } 11 | 12 | // Lets make n equals to sqrt(n) 13 | n = sqrt(n); 14 | // Box check 15 | int si = (i / n) * n; 16 | int sj = (j / n) * n; 17 | 18 | for (int i = si; i < si + n; ++i) 19 | { 20 | for (int j = sj; j < sj + n; ++j) 21 | { 22 | if (board[i][j] == no) return false; 23 | } 24 | } 25 | 26 | return true; 27 | } 28 | 29 | bool sudokuSolver(int board[][9], int i, int j, int n) { 30 | // base case 31 | if (i == n) { 32 | // Print the sudoku 33 | for (int i = 0; i < n; ++i) 34 | { 35 | for (int j = 0; j < n; ++j) 36 | { 37 | cout << board[i][j] << " "; 38 | } 39 | cout << endl; 40 | } 41 | 42 | return true; 43 | } 44 | // recursive case 45 | // What if I fill all the cells of a row 46 | if (j == n) return sudokuSolver(board, i + 1, 0, n); 47 | 48 | // What is I am on already filled cell 49 | if (board[i][j]) { 50 | return sudokuSolver(board, i, j + 1, n); 51 | } 52 | 53 | // Empty cell ka kaam 54 | for (int no = 1; no <= n; ++no) 55 | { 56 | if (isSafe(board, i, j, n, no)) { 57 | board[i][j] = no; 58 | bool kyaBakiSolveHua = sudokuSolver(board, i, j + 1, n); 59 | if (kyaBakiSolveHua) return true; 60 | 61 | board[i][j] = 0; // backtracking 62 | } 63 | } 64 | return false; 65 | } 66 | 67 | int main() { 68 | 69 | int board[9][9] = { 70 | {5, 3, 0, 0, 7, 0, 0, 0, 0}, 71 | {6, 0, 0, 1, 9, 5, 0, 0, 0}, 72 | {0, 9, 8, 0, 0, 0, 0, 6, 0}, 73 | {8, 0, 0, 0, 6, 0, 0, 0, 3}, 74 | {4, 0, 0, 8, 0, 3, 0, 0, 1}, 75 | {7, 0, 0, 0, 2, 0, 0, 0, 6}, 76 | {0, 6, 0, 0, 0, 0, 2, 8, 0}, 77 | {0, 0, 0, 4, 1, 9, 0, 0, 5}, 78 | {0, 0, 0, 0, 8, 0, 0, 7, 9} 79 | }; 80 | 81 | sudokuSolver(board, 0, 0, 9); 82 | 83 | return 0; 84 | } 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /L12-Recursion/2_PhoneKeypad: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L12-Recursion/2_PhoneKeypad -------------------------------------------------------------------------------- /L12-Recursion/2_PhoneKeypad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char keys[][10] = { 5 | "", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ" 6 | }; 7 | 8 | void phoneKeypad(char *ip, int i, char*op, int j) { 9 | // base case 10 | if (ip[i] == '\0') { 11 | op[j] = '\0'; 12 | cout << op << endl; 13 | return; 14 | } 15 | // recursive case 16 | int digit = ip[i] - '0'; 17 | for (int k = 0; keys[digit][k] != '\0'; ++k) 18 | { 19 | op[j] = keys[digit][k]; 20 | phoneKeypad(ip, i + 1, op, j + 1); 21 | } 22 | } 23 | 24 | int main() { 25 | char op[100]; 26 | char ip[] = "23"; 27 | 28 | phoneKeypad(ip, 0, op, 0); 29 | 30 | 31 | /* 32 | int digit = 7; 33 | cout << keys[digit] << endl; 34 | // keys[2] this is an array 35 | for (int k = 0 ; keys[digit][k] != '\0' ; k++) { 36 | cout << keys[digit][k] << "_"; 37 | } 38 | */ 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /L12-Recursion/3_NStairs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L12-Recursion/3_NStairs -------------------------------------------------------------------------------- /L12-Recursion/3_NStairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int nstairs(int n, int k) { 5 | // base case 6 | if (n == 0) return 1; 7 | if (n < 0) return 0; 8 | // recursive case 9 | int ans = 0; 10 | for (int i = 1; i <= k; ++i) 11 | { 12 | ans += nstairs(n - i, k); 13 | } 14 | return ans; 15 | } 16 | 17 | int main() { 18 | 19 | int n = 4, k = 3; 20 | 21 | cout << nstairs(n, k) << endl; 22 | 23 | 24 | return 0; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /L12-Recursion/4_CountWays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L12-Recursion/4_CountWays -------------------------------------------------------------------------------- /L12-Recursion/4_CountWays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ways(int i, int j) { 5 | if (!i or !j) return 1; 6 | 7 | return ways(i - 1, j) + ways(i, j - 1); 8 | } 9 | 10 | 11 | int main() { 12 | 13 | cout << ways(3, 3) << endl; 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /L12-Recursion/5_ElephantWays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L12-Recursion/5_ElephantWays -------------------------------------------------------------------------------- /L12-Recursion/5_ElephantWays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ways(int i, int j) { 5 | if (!i and !j) return 1; 6 | 7 | int ans = 0; 8 | for (int k = 0; k < i; ++k) 9 | { 10 | ans += ways(k, j); 11 | } 12 | 13 | for (int k = 0; k < j; ++k) 14 | { 15 | ans += ways(i, k); 16 | } 17 | 18 | return ans; 19 | } 20 | 21 | int main() { 22 | 23 | cout << ways(5, 8) << endl; 24 | return 0; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /L12-Recursion/6_BalancedParanthesis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L12-Recursion/6_BalancedParanthesis -------------------------------------------------------------------------------- /L12-Recursion/6_BalancedParanthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void balancedParanthesis(char *a, int open, int close, int i, int n) { 5 | // base case 6 | if (i == 2 * n) { 7 | a[i] = '\0'; 8 | cout << a << endl; 9 | return; 10 | } 11 | // recursive case 12 | if (open < n) { 13 | a[i] = '('; 14 | balancedParanthesis(a, open + 1, close, i + 1, n); 15 | } 16 | 17 | if (close < open) { 18 | a[i] = ')'; 19 | balancedParanthesis(a, open, close + 1, i + 1, n); 20 | } 21 | } 22 | 23 | int main() { 24 | 25 | int n = 2; 26 | char a[100]; 27 | balancedParanthesis(a, 0, 0, 0, n); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /L12-Recursion/7_PalindromePartitioning: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L12-Recursion/7_PalindromePartitioning -------------------------------------------------------------------------------- /L12-Recursion/7_PalindromePartitioning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isPalindrome(string s) { 6 | int i = 0, j = s.size() - 1; 7 | while (i < j) { 8 | if (s[i] != s[j]) return false; 9 | i++; 10 | j--; 11 | } 12 | return true; 13 | } 14 | 15 | void palindromePartition(string s, vector &op) { 16 | // base case 17 | if (s.size() == 0) { 18 | for (int i = 0 ; i < op.size(); i++) cout << op[i] << ", "; 19 | cout << endl; 20 | return; 21 | } 22 | 23 | // recursive case 24 | for (int i = 1; i <= s.size(); i++) { 25 | string iskaKaamWeWillDo = s.substr(0, i); 26 | string iskaKaamRecWillDo = s.substr(i); 27 | 28 | if (isPalindrome(iskaKaamWeWillDo)) { 29 | op.push_back(iskaKaamWeWillDo); 30 | palindromePartition(iskaKaamRecWillDo, op); 31 | op.pop_back(); 32 | } 33 | } 34 | } 35 | 36 | int main() { 37 | 38 | string s = "aaba"; 39 | vector op; 40 | palindromePartition(s, op); 41 | 42 | /* 43 | string s = "aaba"; 44 | // s.substr(index, number_of_characters) 45 | for (int i = 1; i <= s.size(); ++i) 46 | { 47 | cout << "Is string ka kaam hum krenge: " << s.substr(0, i) << ", Recursion: " << s.substr(i) << endl; 48 | } 49 | */ 50 | return 0; 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /L13-LinkedList/1_Basics: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/1_Basics -------------------------------------------------------------------------------- /L13-LinkedList/1_Basics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | void printLLByReference(node* &head) { 15 | while (head != NULL) { 16 | cout << head->data << "-->"; 17 | head = head->next; 18 | } 19 | } 20 | 21 | void printLLByValue(node* head) { 22 | while (head != NULL) { 23 | cout << head->data << "-->"; 24 | head = head->next; 25 | } 26 | } 27 | 28 | 29 | int main() { 30 | node a, b, c; 31 | // a, b and c are objects of class node 32 | a.data = 1; 33 | b.data = 2; 34 | c.data = 3; 35 | 36 | a.next = &b; 37 | b.next = &c; 38 | c.next = NULL; 39 | 40 | node *head, *tail; 41 | head = &a; 42 | tail = &c; 43 | 44 | printLLByReference(head); 45 | // printLLByValue(head); 46 | 47 | cout << '\n' << &a << endl; 48 | cout << head << endl; 49 | 50 | 51 | 52 | 53 | 54 | 55 | return 0; 56 | } 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /L13-LinkedList/2_CreateLL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/2_CreateLL -------------------------------------------------------------------------------- /L13-LinkedList/2_CreateLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | int lengthLL(node* head) { 15 | int cnt = 0; 16 | while (head != NULL) { 17 | cnt++; 18 | 19 | head = head->next; 20 | } 21 | 22 | return cnt; 23 | } 24 | 25 | void printLL(node* head) { 26 | while (head != NULL) { 27 | cout << head->data << "-->"; 28 | head = head->next; 29 | } 30 | cout << "NULL\n"; 31 | } 32 | 33 | void insertAtFront(node* &head, node* &tail, int data) { 34 | if (head == NULL) { 35 | node* n = new node(data); 36 | head = tail = n; 37 | } 38 | else { 39 | node* n = new node(data); 40 | n->next = head; 41 | head = n; 42 | } 43 | } 44 | 45 | void insertAtEnd(node* &head, node* &tail, int data) { 46 | if (head == NULL) { 47 | node* n = new node(data); 48 | head = tail = n; 49 | } 50 | else { 51 | node* n = new node(data); 52 | tail->next = n; 53 | tail = n; 54 | } 55 | } 56 | 57 | void insertAtMiddle(node* &head, node* &tail, int pos, int data) { 58 | if (pos == 0) { 59 | insertAtFront(head, tail, data); 60 | } 61 | else if (pos >= lengthLL(head)) { 62 | insertAtEnd(head, tail, data); 63 | } 64 | else { 65 | node* temp = head; 66 | for (int i = 0; i < pos - 1; ++i) 67 | { 68 | temp = temp->next; 69 | } 70 | 71 | node* n = new node(data); 72 | n->next = temp->next; 73 | temp->next = n; 74 | } 75 | } 76 | 77 | int main() { 78 | // int *a, *b; // a and b both are pointers 79 | // int *a, b; // a is a pointer and b is an integer 80 | 81 | node* head, *tail; 82 | head = tail = NULL; 83 | 84 | insertAtEnd(head, tail, 1); 85 | insertAtEnd(head, tail, 2); 86 | insertAtEnd(head, tail, 3); 87 | insertAtEnd(head, tail, 4); 88 | insertAtEnd(head, tail, 5); 89 | insertAtMiddle(head, tail, 3, 10); 90 | 91 | printLL(head); 92 | 93 | 94 | return 0; 95 | } 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /L13-LinkedList/2_DeletionInLL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/2_DeletionInLL -------------------------------------------------------------------------------- /L13-LinkedList/2_DeletionInLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | int lengthLL(node* head) { 15 | int cnt = 0; 16 | while (head != NULL) { 17 | cnt++; 18 | 19 | head = head->next; 20 | } 21 | 22 | return cnt; 23 | } 24 | 25 | void printLL(node* head) { 26 | while (head != NULL) { 27 | cout << head->data << "-->"; 28 | head = head->next; 29 | } 30 | cout << "NULL\n"; 31 | } 32 | 33 | void insertAtEnd(node* &head, node* &tail, int data) { 34 | if (head == NULL) { 35 | node* n = new node(data); 36 | head = tail = n; 37 | } 38 | else { 39 | node* n = new node(data); 40 | tail->next = n; 41 | tail = n; 42 | } 43 | } 44 | 45 | void deleteAtFront(node* &head, node* &tail) { 46 | if (head == NULL) { 47 | return; 48 | } 49 | else if (head->next == NULL) { 50 | delete head; 51 | head = tail = NULL; 52 | } 53 | else { 54 | node* temp = head; 55 | head = head->next; 56 | delete temp; 57 | } 58 | } 59 | 60 | void deleteAtEnd(node* &head, node* &tail) { 61 | if (head == NULL) { 62 | return; 63 | } 64 | else if (head->next == NULL) { 65 | delete head; 66 | head = tail = NULL; 67 | } 68 | else { 69 | node* temp = head; 70 | while (temp->next != tail) { 71 | temp = temp->next; 72 | } 73 | 74 | delete tail; 75 | tail = temp; 76 | tail -> next = NULL; 77 | } 78 | } 79 | 80 | void deletionAtMiddle(node* &head, node* &tail, int pos) { 81 | if (pos == 0) { 82 | deleteAtFront(head, tail); 83 | } 84 | else if (pos >= lengthLL(head) - 1) { 85 | deleteAtEnd(head, tail); 86 | } 87 | else { 88 | node* temp = head; 89 | for (int i = 0; i < pos - 1; ++i) 90 | { 91 | temp = temp->next; 92 | } 93 | 94 | node* n = temp->next; 95 | temp->next = n->next; 96 | delete n; 97 | } 98 | } 99 | 100 | 101 | int main() { 102 | 103 | node* head, *tail; 104 | head = tail = NULL; 105 | 106 | insertAtEnd(head, tail, 1); 107 | insertAtEnd(head, tail, 2); 108 | insertAtEnd(head, tail, 3); 109 | insertAtEnd(head, tail, 4); 110 | insertAtEnd(head, tail, 5); 111 | 112 | // deleteAtEnd(head, tail); 113 | deletionAtMiddle(head, tail, 3); 114 | 115 | printLL(head); 116 | 117 | 118 | return 0; 119 | } 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /L13-LinkedList/3_reverseLL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/3_reverseLL -------------------------------------------------------------------------------- /L13-LinkedList/3_reverseLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | void printLL(node* head) { 15 | while (head != NULL) { 16 | cout << head->data << "-->"; 17 | head = head->next; 18 | } 19 | cout << "NULL\n"; 20 | } 21 | 22 | void insertAtEnd(node* &head, node* &tail, int data) { 23 | if (head == NULL) { 24 | node* n = new node(data); 25 | head = tail = n; 26 | } 27 | else { 28 | node* n = new node(data); 29 | tail->next = n; 30 | tail = n; 31 | } 32 | } 33 | 34 | void reverseLL(node* &head, node* &tail) { 35 | node* c, *p, *n; 36 | 37 | c = head; 38 | p = NULL; 39 | 40 | while (c != NULL) { 41 | n = c->next; 42 | c->next = p; 43 | p = c; 44 | c = n; 45 | } 46 | 47 | swap(head, tail); 48 | } 49 | 50 | 51 | 52 | int main() { 53 | 54 | node* head, *tail; 55 | head = tail = NULL; 56 | 57 | insertAtEnd(head, tail, 1); 58 | insertAtEnd(head, tail, 2); 59 | insertAtEnd(head, tail, 3); 60 | insertAtEnd(head, tail, 4); 61 | insertAtEnd(head, tail, 5); 62 | printLL(head); 63 | 64 | reverseLL(head, tail); 65 | 66 | printLL(head); 67 | 68 | 69 | return 0; 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /L13-LinkedList/4_mergeSortedList: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/4_mergeSortedList -------------------------------------------------------------------------------- /L13-LinkedList/4_mergeSortedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | void printLL(node* head) { 15 | while (head != NULL) { 16 | cout << head->data << "-->"; 17 | head = head->next; 18 | } 19 | cout << "NULL\n"; 20 | } 21 | 22 | void insertAtEnd(node* &head, node* &tail, int data) { 23 | if (head == NULL) { 24 | node* n = new node(data); 25 | head = tail = n; 26 | } 27 | else { 28 | node* n = new node(data); 29 | tail->next = n; 30 | tail = n; 31 | } 32 | } 33 | 34 | node* mergeLL(node* a, node* b) { 35 | if (a == NULL) return b; 36 | if (!b) return a; 37 | 38 | node* nH; 39 | 40 | if (a->data < b->data) { 41 | nH = a; 42 | nH->next = mergeLL(a->next, b); 43 | } 44 | else { 45 | nH = b; 46 | nH->next = mergeLL(a, b->next); 47 | } 48 | 49 | return nH; 50 | } 51 | 52 | 53 | 54 | int main() { 55 | 56 | node* head, *tail; 57 | node* head1, *tail1; 58 | head = tail = NULL; 59 | head1 = tail1 = NULL; 60 | 61 | insertAtEnd(head, tail, 1); 62 | insertAtEnd(head, tail, 3); 63 | insertAtEnd(head, tail, 5); 64 | insertAtEnd(head, tail, 7); 65 | insertAtEnd(head, tail, 8); 66 | printLL(head); 67 | 68 | insertAtEnd(head1, tail1, 2); 69 | insertAtEnd(head1, tail1, 4); 70 | insertAtEnd(head1, tail1, 6); 71 | insertAtEnd(head1, tail1, 9); 72 | 73 | printLL(head1); 74 | 75 | 76 | node* x = mergeLL(head, head1); 77 | printLL(x); 78 | 79 | 80 | return 0; 81 | } 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /L13-LinkedList/5_FindingMiddle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/5_FindingMiddle -------------------------------------------------------------------------------- /L13-LinkedList/5_FindingMiddle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | void printLL(node* head) { 15 | while (head != NULL) { 16 | cout << head->data << "-->"; 17 | head = head->next; 18 | } 19 | cout << "NULL\n"; 20 | } 21 | 22 | void insertAtEnd(node* &head, node* &tail, int data) { 23 | if (head == NULL) { 24 | node* n = new node(data); 25 | head = tail = n; 26 | } 27 | else { 28 | node* n = new node(data); 29 | tail->next = n; 30 | tail = n; 31 | } 32 | } 33 | 34 | node* midLL(node* head) { 35 | if (head == NULL || head->next == NULL) return head; 36 | 37 | node* s = head; 38 | node* f = head->next; 39 | 40 | while (f and f->next) { 41 | f = f->next->next; 42 | s = s->next; 43 | } 44 | 45 | return s; 46 | } 47 | 48 | 49 | int main() { 50 | 51 | node* head, *tail; 52 | head = tail = NULL; 53 | 54 | insertAtEnd(head, tail, 1); 55 | insertAtEnd(head, tail, 2); 56 | insertAtEnd(head, tail, 3); 57 | insertAtEnd(head, tail, 4); 58 | insertAtEnd(head, tail, 5); 59 | insertAtEnd(head, tail, 6); 60 | printLL(head); 61 | 62 | 63 | node* x = midLL(head); 64 | cout << x->data << endl; 65 | 66 | 67 | 68 | return 0; 69 | } 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /L13-LinkedList/6_MergeSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/6_MergeSort -------------------------------------------------------------------------------- /L13-LinkedList/6_MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | int lengthLL(node* head) { 15 | int cnt = 0; 16 | while (head != NULL) { 17 | cnt++; 18 | 19 | head = head->next; 20 | } 21 | 22 | return cnt; 23 | } 24 | 25 | void printLL(node* head) { 26 | while (head != NULL) { 27 | cout << head->data << "-->"; 28 | head = head->next; 29 | } 30 | cout << "NULL\n"; 31 | } 32 | 33 | void insertAtEnd(node* &head, node* &tail, int data) { 34 | if (head == NULL) { 35 | node* n = new node(data); 36 | head = tail = n; 37 | } 38 | else { 39 | node* n = new node(data); 40 | tail->next = n; 41 | tail = n; 42 | } 43 | } 44 | 45 | node* midLL(node* head) { 46 | if (head == NULL || head->next == NULL) return head; 47 | 48 | node* s = head; 49 | node* f = head->next; 50 | 51 | while (f and f->next) { 52 | f = f->next->next; 53 | s = s->next; 54 | } 55 | 56 | return s; 57 | } 58 | 59 | node* mergeLL(node* a, node* b) { 60 | if (a == NULL) return b; 61 | if (!b) return a; 62 | 63 | node* nH; 64 | 65 | if (a->data < b->data) { 66 | nH = a; 67 | nH->next = mergeLL(a->next, b); 68 | } 69 | else { 70 | nH = b; 71 | nH->next = mergeLL(a, b->next); 72 | } 73 | 74 | return nH; 75 | } 76 | 77 | // sort karke merko new head return karna hai 78 | node* mergeSort(node* head) { 79 | // base case 80 | if (head == NULL or head->next == NULL) return head; 81 | 82 | // recursive case 83 | // 1. Divide 84 | node* m = midLL(head); 85 | node* a = head; 86 | node* b = m->next; 87 | m->next = NULL; 88 | // 2. Sort 89 | a = mergeSort(a); // choti list ko sort krke recursion bhi naya head return karega 90 | b = mergeSort(b); // choti list ko sort krke recursion bhi naya head return karega 91 | // 3. Merge 92 | node* nH = mergeLL(a, b); 93 | // 4. return new head 94 | return nH; 95 | } 96 | 97 | int main() { 98 | 99 | node* head, *tail; 100 | head = tail = NULL; 101 | 102 | insertAtEnd(head, tail, 11); 103 | insertAtEnd(head, tail, 3); 104 | insertAtEnd(head, tail, 51); 105 | insertAtEnd(head, tail, 7); 106 | insertAtEnd(head, tail, 2); 107 | insertAtEnd(head, tail, 1); 108 | printLL(head); 109 | 110 | node* x = mergeSort(head); 111 | printLL(x); 112 | 113 | 114 | return 0; 115 | } 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /L13-LinkedList/7_FindKthNodeFromLast: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/7_FindKthNodeFromLast -------------------------------------------------------------------------------- /L13-LinkedList/7_FindKthNodeFromLast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | int lengthLL(node* head) { 15 | int cnt = 0; 16 | while (head != NULL) { 17 | cnt++; 18 | 19 | head = head->next; 20 | } 21 | 22 | return cnt; 23 | } 24 | 25 | void printLL(node* head) { 26 | while (head != NULL) { 27 | cout << head->data << "-->"; 28 | head = head->next; 29 | } 30 | cout << "NULL\n"; 31 | } 32 | 33 | void insertAtEnd(node* &head, node* &tail, int data) { 34 | if (head == NULL) { 35 | node* n = new node(data); 36 | head = tail = n; 37 | } 38 | else { 39 | node* n = new node(data); 40 | tail->next = n; 41 | tail = n; 42 | } 43 | } 44 | 45 | node* findKLast(node* head, int k) { 46 | node* s = head, *f = head; 47 | 48 | for (int i = 0; i < k; ++i) 49 | { 50 | f = f->next; 51 | } 52 | 53 | while (f != NULL) { 54 | f = f->next; 55 | s = s->next; 56 | } 57 | 58 | return s; 59 | } 60 | 61 | int main() { 62 | 63 | node* head, *tail; 64 | head = tail = NULL; 65 | 66 | insertAtEnd(head, tail, 11); 67 | insertAtEnd(head, tail, 3); 68 | insertAtEnd(head, tail, 51); 69 | insertAtEnd(head, tail, 7); 70 | insertAtEnd(head, tail, 2); 71 | insertAtEnd(head, tail, 1); 72 | printLL(head); 73 | 74 | node* x = findKLast(head, 4); 75 | cout << x->data << endl; 76 | 77 | 78 | return 0; 79 | } 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /L13-LinkedList/8_CycleDetection: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L13-LinkedList/8_CycleDetection -------------------------------------------------------------------------------- /L13-LinkedList/8_CycleDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { // Node banani hai toh constructor se banwaenge 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | int lengthLL(node* head) { 15 | int cnt = 0; 16 | while (head != NULL) { 17 | cnt++; 18 | 19 | head = head->next; 20 | } 21 | 22 | return cnt; 23 | } 24 | 25 | void printLL(node* head) { 26 | while (head != NULL) { 27 | cout << head->data << "-->"; 28 | head = head->next; 29 | } 30 | cout << "NULL\n"; 31 | } 32 | 33 | void insertAtEnd(node* &head, node* &tail, int data) { 34 | if (head == NULL) { 35 | node* n = new node(data); 36 | head = tail = n; 37 | } 38 | else { 39 | node* n = new node(data); 40 | tail->next = n; 41 | tail = n; 42 | } 43 | } 44 | 45 | void breakTheCycle(node* head, node* f) { 46 | node* s = head; 47 | 48 | node* fp = head; 49 | while (fp->next != f) { 50 | fp = fp->next; 51 | } 52 | 53 | while (f != s) { 54 | fp = f; 55 | f = f->next; 56 | s = s->next; 57 | } 58 | fp->next = NULL; 59 | } 60 | 61 | bool isCyclic(node* head) { 62 | node* f = head, *s = head; 63 | 64 | while (f and f->next) { 65 | f = f->next->next; 66 | s = s->next; 67 | 68 | if (f == s) { 69 | breakTheCycle(head, f); 70 | return true; 71 | } 72 | } 73 | 74 | return false; 75 | } 76 | 77 | 78 | int main() { 79 | 80 | node* head, *tail; 81 | head = tail = NULL; 82 | 83 | insertAtEnd(head, tail, 1); 84 | insertAtEnd(head, tail, 2); 85 | insertAtEnd(head, tail, 3); 86 | insertAtEnd(head, tail, 4); 87 | insertAtEnd(head, tail, 5); 88 | insertAtEnd(head, tail, 6); 89 | insertAtEnd(head, tail, 7); 90 | insertAtEnd(head, tail, 8); 91 | 92 | printLL(head); 93 | 94 | tail->next = head->next->next->next; 95 | 96 | 97 | if (isCyclic(head)) cout << "Cyclic\n"; 98 | else cout << "Not Cyclic\n"; 99 | 100 | printLL(head); 101 | 102 | 103 | 104 | return 0; 105 | } 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/1_Linkedlist-K-Reverse.cpp: -------------------------------------------------------------------------------- 1 | #define ListNode node 2 | class Solution { 3 | public: 4 | node* reverseLL(node*head, int k) { 5 | node* c = head, *p = NULL, *n; 6 | 7 | for (int i = 0; i < k; ++i) 8 | { 9 | n = c->next; 10 | c->next = p; 11 | p = c; 12 | c = n; 13 | } 14 | 15 | return p; // We are expected to return the newHead after reversal of LL 16 | } 17 | 18 | node* reverseKGroup(node* head, int k) { 19 | // Create temp to prevent memory leak 20 | node* temp = head, *og = NULL, *p = NULL; 21 | 22 | while (true) { 23 | // 1. Jump karwaao temp ko k times, only and only if temp can jump k times 24 | 25 | int jumpCnt = 0; 26 | while (jumpCnt < k and temp != NULL) { 27 | temp = temp->next; 28 | jumpCnt++; 29 | } 30 | 31 | if (jumpCnt == k) { 32 | node* nH = reverseLL(head, k); 33 | if (og == NULL) og = nH; 34 | head->next = temp; 35 | if (p) p->next = nH; 36 | p = head; 37 | head = temp; 38 | } 39 | else break; 40 | } 41 | 42 | return og; 43 | } 44 | }; 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/1_Pattern-PenPaperRound: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L14-StackAndQueue-Discussion/1_Pattern-PenPaperRound -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/1_Pattern-PenPaperRound.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve(int no, int inc) { 5 | if (no == 53) { 6 | cout << no << " "; 7 | return; 8 | } 9 | 10 | cout << no << " "; 11 | solve(no + inc, inc + 4); 12 | cout << no + inc << " "; 13 | } 14 | 15 | int main() { 16 | solve(3, 2); 17 | 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/2_OddEvenLL.cpp: -------------------------------------------------------------------------------- 1 | #define node ListNode 2 | class Solution { 3 | public: 4 | 5 | node* oddEvenList(node* head) { 6 | node* oh = NULL, *ot = NULL, *eh = NULL, *et = NULL; 7 | 8 | while (head) { 9 | if ( (head->val) % 2) { 10 | if (oh == NULL) { 11 | oh = ot = head; 12 | head = head->next; 13 | ot->next = NULL; 14 | } 15 | else { 16 | ot->next = head; 17 | ot = head; 18 | head = head->next; 19 | ot->next = NULL; 20 | } 21 | } 22 | else { 23 | if (eh == NULL) { 24 | eh = et = head; 25 | head = head->next; 26 | et->next = NULL; 27 | } 28 | else { 29 | et->next = head; 30 | et = head; 31 | head = head->next; 32 | et->next = NULL; 33 | } 34 | } 35 | } 36 | 37 | if (oh != NULL) { 38 | ot->next = eh; 39 | return oh; 40 | } 41 | else { 42 | return eh; 43 | } 44 | 45 | } 46 | 47 | }; -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/2_Pattern-PenPaper-SalescodeAI: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L14-StackAndQueue-Discussion/2_Pattern-PenPaper-SalescodeAI -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/2_Pattern-PenPaper-SalescodeAI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve(int no, int inc) { 5 | if (no == 12) { 6 | cout << no << " "; 7 | return; 8 | } 9 | 10 | cout << no << " "; 11 | solve(no + inc, inc); 12 | if ((no + inc) % 4 == 0) 13 | cout << no + inc << " "; 14 | } 15 | 16 | int main() { 17 | solve(2, 2); 18 | 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/3_OddEvenIndex.cpp: -------------------------------------------------------------------------------- 1 | #define node ListNode 2 | class Solution { 3 | public: 4 | 5 | node* oddEvenList(node* head) { 6 | node* oh = NULL, *ot = NULL, *eh = NULL, *et = NULL; 7 | int i = 0; 8 | while (head) { 9 | if ( i % 2 == 0) { 10 | if (oh == NULL) { 11 | oh = ot = head; 12 | head = head->next; 13 | ot->next = NULL; 14 | } 15 | else { 16 | ot->next = head; 17 | ot = head; 18 | head = head->next; 19 | ot->next = NULL; 20 | } 21 | } 22 | else { 23 | if (eh == NULL) { 24 | eh = et = head; 25 | head = head->next; 26 | et->next = NULL; 27 | } 28 | else { 29 | et->next = head; 30 | et = head; 31 | head = head->next; 32 | et->next = NULL; 33 | } 34 | } 35 | 36 | i++; 37 | } 38 | 39 | if (oh != NULL) { 40 | ot->next = eh; 41 | return oh; 42 | } 43 | else { 44 | return eh; 45 | } 46 | 47 | } 48 | 49 | }; -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/4_RemoveDuplicates.cpp: -------------------------------------------------------------------------------- 1 | #define node ListNode 2 | class Solution { 3 | public: 4 | node* deleteDuplicates(node* head) { 5 | node* h = NULL, * t = NULL; 6 | // ek bhi node nahi hai, ya ek hi node hai toh duplicates possible nhi hai 7 | if (head == NULL || head->next == NULL) return head; 8 | 9 | while (head and head->next) { 10 | if (head->val != head->next->val) { 11 | if (h == NULL) { 12 | h = t = head; 13 | head = head->next; 14 | t->next = NULL; 15 | } 16 | else { 17 | t->next = head; 18 | t = t->next; 19 | head = head->next; 20 | t->next = NULL; 21 | } 22 | } 23 | else { 24 | int val = head->val; 25 | while (head and head->val == val) { 26 | head = head->next; 27 | } 28 | } 29 | } 30 | 31 | t->next = head; 32 | return h; 33 | } 34 | 35 | 36 | }; -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/5_Stack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L14-StackAndQueue-Discussion/5_Stack -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/5_Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // STL of stack 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | stack s; 8 | 9 | for (int i = 1; i < 5; ++i) 10 | { 11 | s.push(i); 12 | } 13 | 14 | while (!s.empty()) { 15 | cout << s.top() << " "; 16 | s.pop(); 17 | } 18 | 19 | cout << endl; 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/6_StackVector: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L14-StackAndQueue-Discussion/6_StackVector -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/6_StackVector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Stack { 6 | public: 7 | vector v; 8 | 9 | void push(int d) { 10 | v.push_back(d); 11 | } 12 | 13 | void pop() { 14 | v.pop_back(); 15 | } 16 | 17 | int top() { 18 | return v[v.size() - 1]; 19 | } 20 | 21 | bool empty() { 22 | return v.size() == 0; 23 | } 24 | }; 25 | 26 | int main() { 27 | Stack s; 28 | 29 | for (int i = 1; i < 5; ++i) 30 | { 31 | s.push(i); 32 | } 33 | 34 | while (!s.empty()) { 35 | cout << s.top() << " "; 36 | s.pop(); 37 | } 38 | 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 | -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/7_StackUsingLL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L14-StackAndQueue-Discussion/7_StackUsingLL -------------------------------------------------------------------------------- /L14-StackAndQueue-Discussion/7_StackUsingLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | 15 | class Stack { 16 | public: 17 | node* head; 18 | 19 | Stack() { 20 | head = NULL; 21 | } 22 | 23 | void push(int d) { 24 | node* n = new node(d); 25 | if (!head) { 26 | head = n; 27 | } 28 | else { 29 | n->next = head; 30 | head = n; 31 | } 32 | } 33 | 34 | void pop() { 35 | if (!head) return; 36 | else if (head->next == NULL) { // This means only single node is present 37 | delete head; 38 | head = NULL; 39 | } 40 | else { 41 | node* t = head; 42 | head = head->next; 43 | delete t; 44 | } 45 | } 46 | 47 | int top() { 48 | return head->data; 49 | } 50 | 51 | bool empty() { 52 | return head == NULL; 53 | } 54 | }; 55 | 56 | int main() { 57 | 58 | Stack s; 59 | 60 | 61 | for (int i = 1; i < 5; ++i) 62 | { 63 | s.push(i); 64 | } 65 | 66 | while (!s.empty()) { 67 | cout << s.top() << " "; 68 | s.pop(); 69 | } 70 | 71 | cout << endl; 72 | 73 | return 0; 74 | } 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /L16-Trees/10_ZigZagTraversalPrint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/10_ZigZagTraversalPrint -------------------------------------------------------------------------------- /L16-Trees/10_ZigZagTraversalPrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left, *right; 9 | node(int d) { 10 | data = d; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | node* buildTree() { 16 | int data; 17 | cin >> data; 18 | if (data == -1) return NULL; 19 | 20 | node* root = new node(data); 21 | // LST 22 | root->left = buildTree(); 23 | // RST 24 | root->right = buildTree(); 25 | 26 | return root; 27 | } 28 | 29 | void zigZagTraversal(node* root) { 30 | queue q; 31 | 32 | q.push(root); 33 | q.push(NULL); 34 | vector > v; 35 | 36 | vector temp; 37 | 38 | while (!q.empty()) { 39 | node* f = q.front(); 40 | q.pop(); 41 | 42 | if (f) { 43 | // cout << f->data << " "; 44 | temp.push_back(f->data); 45 | if (f->left) q.push(f->left); 46 | if (f->right) q.push(f->right); 47 | } 48 | else { 49 | v.push_back(temp); 50 | temp.clear(); 51 | 52 | if (!q.empty()) q.push(NULL); 53 | } 54 | } 55 | 56 | 57 | for (int i = 0 ; i < v.size(); i ++) { 58 | if (i % 2) { // Odd hai i 59 | for (int j = v[i].size() - 1; j >= 0; --j) 60 | { 61 | cout << v[i][j] << " "; 62 | } 63 | } 64 | else { 65 | for (int j = 0; j < v[i].size(); ++j) 66 | { 67 | cout << v[i][j] << " "; 68 | } 69 | } 70 | cout << endl; 71 | } 72 | cout << endl; 73 | 74 | } 75 | 76 | int main() { 77 | 78 | node* root = buildTree(); 79 | 80 | zigZagTraversal(root); 81 | 82 | return 0; 83 | } 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /L16-Trees/1_Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/1_Test -------------------------------------------------------------------------------- /L16-Trees/1_Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | void preOrder(node* root) { 29 | if (!root) return; 30 | 31 | cout << root->data << " "; 32 | preOrder(root->left); 33 | preOrder(root->right); 34 | } 35 | 36 | 37 | void inorder(node* root) { 38 | if (!root) return; 39 | 40 | inorder(root->left); 41 | cout << root->data << " "; 42 | inorder(root->right); 43 | } 44 | 45 | 46 | void postOrder(node* root) { 47 | if (!root) return; 48 | 49 | postOrder(root->left); 50 | postOrder(root->right); 51 | cout << root->data << " "; 52 | } 53 | 54 | 55 | int main() { 56 | 57 | node* root = buildTree(); 58 | preOrder(root); 59 | cout << endl; 60 | inorder(root); 61 | cout << endl; 62 | postOrder(root); 63 | cout << endl; 64 | 65 | 66 | return 0; 67 | } 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /L16-Trees/2_CountNodes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/2_CountNodes -------------------------------------------------------------------------------- /L16-Trees/2_CountNodes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | int countNodes(node*root) { 29 | if (!root) return 0; 30 | 31 | return 1 + countNodes(root->left) + countNodes(root->right); 32 | } 33 | 34 | int main() { 35 | 36 | node* root = buildTree(); 37 | cout << countNodes(root) << endl; 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /L16-Trees/3_HeightOfTree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/3_HeightOfTree -------------------------------------------------------------------------------- /L16-Trees/3_HeightOfTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | int height(node*root) { 29 | if (!root) return 0; 30 | 31 | return 1 + max(height(root->left), height(root->right)); 32 | } 33 | 34 | int main() { 35 | 36 | node* root = buildTree(); 37 | cout << height(root) << endl; 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /L16-Trees/4_Diameter: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/4_Diameter -------------------------------------------------------------------------------- /L16-Trees/4_Diameter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | int height(node*root) { 29 | if (!root) return 0; 30 | 31 | return 1 + max(height(root->left), height(root->right)); 32 | } 33 | 34 | int diameter(node* root) { 35 | if (!root) return 0; 36 | 37 | int op1 = height(root->left) + height(root->right); 38 | int op2 = diameter(root->left); 39 | int op3 = diameter(root->right); 40 | 41 | return max(op1, max(op2, op3)); 42 | } 43 | 44 | 45 | class Pair { 46 | public: 47 | int height; 48 | int diameter; 49 | }; 50 | 51 | Pair fastDiameter(node* root) { 52 | if (!root) { 53 | Pair p; 54 | p.height = p.diameter = 0; 55 | return p; 56 | } 57 | 58 | Pair p; 59 | Pair left = fastDiameter(root->left); 60 | Pair right = fastDiameter(root->right); 61 | 62 | p.height = max(left.height, right.height) + 1; 63 | int op1 = left.height + right.height; 64 | int op2 = left.diameter; 65 | int op3 = right.diameter; 66 | 67 | p.diameter = max(op1, max(op2, op3)); 68 | return p; 69 | } 70 | 71 | int main() { 72 | 73 | node* root = buildTree(); 74 | cout << diameter(root) << endl; 75 | 76 | Pair p = fastDiameter(root); 77 | cout << "Fast Height " << p.height << endl; 78 | cout << "Fast Diameter " << p.diameter << endl; 79 | 80 | return 0; 81 | } 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /L16-Trees/5_FindNode: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/5_FindNode -------------------------------------------------------------------------------- /L16-Trees/5_FindNode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | node* search(node* root, int key) { 29 | if (!root) return NULL; 30 | 31 | if (root->data == key) return root; 32 | 33 | node* left = search(root->left, key); 34 | if (left != NULL) return left; // Agar left side se key wali node ka address aa gaya toh done 35 | 36 | node* right = search(root->right, key); 37 | // if (right != NULL) return right; 38 | 39 | return right; // right se answer aaya toh address hoga, nhi aaya toh usmei NULL hoga 40 | } 41 | 42 | 43 | int main() { 44 | 45 | node* root = buildTree(); 46 | 47 | node* ans = search(root, 113); 48 | if (ans)cout << "Found: " << ans->data << endl; 49 | else cout << "Not Found\n"; 50 | 51 | return 0; 52 | } 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /L16-Trees/6_MirrorBT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/6_MirrorBT -------------------------------------------------------------------------------- /L16-Trees/6_MirrorBT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | void mirror(node* root) { 29 | if (!root) return; 30 | 31 | swap(root->left, root->right); 32 | mirror(root->left); // LST ka kaam recursion ko boldo 33 | mirror(root->right); // RST ka kaam recursion ko boldo 34 | } 35 | 36 | void preOrder(node* root) { 37 | if (!root) return; 38 | 39 | cout << root->data << " "; 40 | preOrder(root->left); 41 | preOrder(root->right); 42 | } 43 | 44 | void inorder(node* root) { 45 | if (!root) return; 46 | 47 | inorder(root->left); 48 | cout << root->data << " "; 49 | inorder(root->right); 50 | } 51 | 52 | 53 | int main() { 54 | 55 | node* root = buildTree(); 56 | mirror(root); 57 | preOrder(root); 58 | cout << endl; 59 | inorder(root); 60 | cout << endl; 61 | 62 | return 0; 63 | } 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /L16-Trees/7_Levelorder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/7_Levelorder -------------------------------------------------------------------------------- /L16-Trees/7_Levelorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left, *right; 9 | node(int d) { 10 | data = d; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | node* buildTree() { 16 | int data; 17 | cin >> data; 18 | if (data == -1) return NULL; 19 | 20 | node* root = new node(data); 21 | // LST 22 | root->left = buildTree(); 23 | // RST 24 | root->right = buildTree(); 25 | 26 | return root; 27 | } 28 | 29 | void levelOrder(node* root) { 30 | queue q; 31 | 32 | q.push(root); 33 | q.push(NULL); 34 | 35 | while (!q.empty()) { 36 | node* f = q.front(); 37 | q.pop(); 38 | 39 | if (f) { 40 | cout << f->data << " "; 41 | if (f->left) q.push(f->left); 42 | if (f->right) q.push(f->right); 43 | } 44 | else { 45 | cout << endl; 46 | if (!q.empty()) q.push(NULL); 47 | } 48 | } 49 | } 50 | 51 | int main() { 52 | 53 | node* root = buildTree(); 54 | 55 | levelOrder(root); 56 | 57 | return 0; 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /L16-Trees/8_InputTreeLevelWise: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/8_InputTreeLevelWise -------------------------------------------------------------------------------- /L16-Trees/8_InputTreeLevelWise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left, *right; 9 | node(int d) { 10 | data = d; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | node* buildLevelWiseTree() { 16 | 17 | int data; 18 | cout << "Enter root data : "; 19 | cin >> data; 20 | if (data == -1) return NULL; 21 | 22 | node* root = new node(data); 23 | queue q; 24 | q.push(root); 25 | 26 | // Take input of tree 27 | while (!q.empty()) { 28 | node* f = q.front(); 29 | q.pop(); 30 | 31 | cout << "Enter children of " << f->data << " : "; 32 | int l, r; 33 | cin >> l >> r; 34 | if (l != -1) f->left = new node(l), q.push(f->left); 35 | if (r != -1) f->right = new node(r), q.push(f->right); 36 | } 37 | 38 | 39 | return root; 40 | } 41 | 42 | 43 | void levelOrderPrint(node* root) { 44 | queue q; 45 | 46 | q.push(root); 47 | q.push(NULL); 48 | 49 | while (!q.empty()) { 50 | node* f = q.front(); 51 | q.pop(); 52 | 53 | if (f) { 54 | cout << f->data << " "; 55 | if (f->left) q.push(f->left); 56 | if (f->right) q.push(f->right); 57 | } 58 | else { 59 | cout << endl; 60 | if (!q.empty()) q.push(NULL); 61 | } 62 | } 63 | } 64 | 65 | 66 | int main() { 67 | 68 | node* root = buildLevelWiseTree(); 69 | 70 | levelOrderPrint(root); 71 | 72 | return 0; 73 | } 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /L16-Trees/9_RightViewOfTree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L16-Trees/9_RightViewOfTree -------------------------------------------------------------------------------- /L16-Trees/9_RightViewOfTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | void rightViewTree(node* root, int l, int &maxLevel) { 29 | if (!root) return; 30 | 31 | if (l > maxLevel) { 32 | cout << root->data << " "; 33 | maxLevel = l; 34 | } 35 | 36 | rightViewTree(root->right, l + 1, maxLevel); 37 | rightViewTree(root->left, l + 1, maxLevel); 38 | } 39 | 40 | 41 | void leftViewTree(node* root, int l, int &maxLevel) { 42 | if (!root) return; 43 | 44 | if (l > maxLevel) { 45 | cout << root->data << " "; 46 | maxLevel = l; 47 | } 48 | 49 | leftViewTree(root->left, l + 1, maxLevel); 50 | leftViewTree(root->right, l + 1, maxLevel); 51 | } 52 | 53 | 54 | int main() { 55 | 56 | node* root = buildTree(); 57 | int maxLevel = -1; 58 | leftViewTree(root, 0, maxLevel); 59 | 60 | return 0; 61 | } 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /L16-Trees/input.txt: -------------------------------------------------------------------------------- 1 | 8 10 1 -1 -1 6 4 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1 -------------------------------------------------------------------------------- /L17-Trees/1_LCA: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L17-Trees/1_LCA -------------------------------------------------------------------------------- /L17-Trees/1_LCA.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | node* lca(node* root, int a, int b) { 29 | if (!root) return NULL; 30 | 31 | if (root->data == a || root->data == b) return root; 32 | node* leftMeiNodeKaAddress = lca(root->left, a, b); 33 | node* rightMeiNodeKaAddress = lca(root->right, a, b); 34 | 35 | if (leftMeiNodeKaAddress and rightMeiNodeKaAddress) return root; 36 | else if (leftMeiNodeKaAddress) return leftMeiNodeKaAddress; 37 | return rightMeiNodeKaAddress; 38 | } 39 | 40 | int main() { 41 | 42 | node* root = buildTree(); 43 | node* ans = lca(root, 3, 13); 44 | 45 | if (ans) cout << ans->data << endl; 46 | else cout << "No LCA possible\n"; 47 | 48 | 49 | return 0; 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /L17-Trees/2_NodeAtK: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L17-Trees/2_NodeAtK -------------------------------------------------------------------------------- /L17-Trees/2_NodeAtK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* buildTree() { 15 | int data; 16 | cin >> data; 17 | if (data == -1) return NULL; 18 | 19 | node* root = new node(data); 20 | // LST 21 | root->left = buildTree(); 22 | // RST 23 | root->right = buildTree(); 24 | 25 | return root; 26 | } 27 | 28 | void solve(node* root, int k, int d = 0) { 29 | if (!root) return ; 30 | 31 | if (d == k) { 32 | cout << root->data << " "; 33 | return; 34 | } 35 | 36 | solve(root->left, k, d + 1); 37 | solve(root->right, k, d + 1); 38 | } 39 | 40 | int distanceOfNode(node* root, int node, int k) { 41 | if (!root) return -1; 42 | 43 | if (root->data == node) { 44 | solve(root, k); // Agar node mill gai toh tree ke neeche wale sabhi nodes at 45 | // distance k print karo 46 | return 0; 47 | } 48 | 49 | // LST ki root se node ka distance batao 50 | int d = distanceOfNode(root->left, node, k); 51 | if (d != -1) { // Valid distance hai 52 | if (d + 1 == k) { 53 | cout << root->data << " "; 54 | return -1; // Ab aur upar distance k par nodes nahi milengi 55 | } 56 | 57 | int rd = k - d - 2; 58 | solve(root->right, rd); // Ab right subtree par rd distance wali nodes print kardo 59 | return d + 1; 60 | } 61 | 62 | // Ab RST se node ka distance pucho 63 | d = distanceOfNode(root->right, node, k); 64 | if (d != -1) { 65 | if (d + 1 == k) { 66 | cout << root->data << " "; 67 | return -1; // Ab aur upar distance k par nodes nahi milengi 68 | } 69 | 70 | int rd = k - d - 2; 71 | solve(root->left, rd); // Ab left subtree par rd distance wali nodes print kardo 72 | return d + 1; 73 | } 74 | 75 | return -1; 76 | } 77 | 78 | int main() { 79 | 80 | node* root = buildTree(); 81 | 82 | distanceOfNode(root, 10, 2); 83 | 84 | 85 | return 0; 86 | } 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /L17-Trees/3_HouseRobber3.cpp: -------------------------------------------------------------------------------- 1 | class Pair { 2 | public: 3 | int choriKari, choriNahiKari; 4 | }; 5 | 6 | class Solution { 7 | public: 8 | Pair solve(TreeNode* root) { 9 | if (!root) return {0, 0}; 10 | 11 | Pair left = solve(root->left); 12 | Pair right = solve(root->right); 13 | 14 | Pair p; 15 | p.choriKari = root->data + left.choriNahiKari + right.choriNahiKari; 16 | p.choriNahiKari = max(left.choriKari, left.choriNahiKari) + 17 | max(right.choriKari, right.choriNahiKari); 18 | return p; 19 | } 20 | 21 | int rob(TreeNode* root) { 22 | Pair ans = solve(root); 23 | return max(ans.choriNahiKari, ans.choriKari); 24 | } 25 | }; -------------------------------------------------------------------------------- /L17-Trees/4_BinaryTreeCamera.cpp: -------------------------------------------------------------------------------- 1 | #define node TreeNode 2 | #define cameraNahiLaga 0 3 | #define cameraLagaHai 1 4 | #define underVigilance 2 5 | 6 | class Solution { 7 | public: 8 | int cameraCount = 0; 9 | int solve(node* root) { 10 | if (!root) return underVigilance; 11 | 12 | int leftSays = solve(root->left); 13 | int rightSays = solve(root->right); 14 | 15 | if (leftSays == cameraNahiLaga || rightSays == cameraNahiLaga) { 16 | cameraCount++; 17 | return cameraLagaHai; 18 | } 19 | else if (leftSays == cameraLagaHai || rightSays == cameraLagaHai) { 20 | return underVigilance; 21 | } 22 | else { 23 | // Both the children are underVigilance 24 | return cameraNahiLaga; 25 | } 26 | } 27 | 28 | int minCameraCover(node* root) { 29 | int rootState = solve(root); 30 | if (rootState == cameraNahiLaga) cameraCount++; 31 | return cameraCount; 32 | } 33 | }; 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /L17-Trees/input.txt: -------------------------------------------------------------------------------- 1 | 8 10 1 -1 -1 6 4 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1 -------------------------------------------------------------------------------- /L18-BST/1_BST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L18-BST/1_BST -------------------------------------------------------------------------------- /L18-BST/1_BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left, *right; 9 | node(int d) { 10 | data = d; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | node* insertInBST(node* root, int data) { 16 | if (!root) { 17 | root = new node(data); 18 | return root; 19 | } 20 | 21 | if (data < root->data) { 22 | root->left = insertInBST(root->left, data); 23 | } 24 | else { 25 | root->right = insertInBST(root->right, data); 26 | } 27 | return root; 28 | } 29 | 30 | node* buildBST() { 31 | 32 | node* root = NULL; 33 | 34 | 35 | int data; 36 | cin >> data; 37 | while (data != -1) { 38 | root = insertInBST(root, data); 39 | 40 | cin >> data; 41 | } 42 | 43 | return root; 44 | } 45 | 46 | 47 | void levelOrder(node* root) { 48 | queue q; 49 | 50 | q.push(root); 51 | q.push(NULL); 52 | 53 | while (!q.empty()) { 54 | node* f = q.front(); 55 | q.pop(); 56 | 57 | if (f) { 58 | cout << f->data << " "; 59 | if (f->left) q.push(f->left); 60 | if (f->right) q.push(f->right); 61 | } 62 | else { 63 | cout << endl; 64 | if (!q.empty()) q.push(NULL); 65 | } 66 | } 67 | } 68 | 69 | void preOrder(node* root) { 70 | if (!root) return; 71 | 72 | cout << root->data << " "; 73 | preOrder(root->left); 74 | preOrder(root->right); 75 | } 76 | 77 | 78 | void inorder(node* root) { 79 | if (!root) return; 80 | 81 | inorder(root->left); 82 | cout << root->data << " "; 83 | inorder(root->right); 84 | } 85 | 86 | 87 | void postOrder(node* root) { 88 | if (!root) return; 89 | 90 | postOrder(root->left); 91 | postOrder(root->right); 92 | cout << root->data << " "; 93 | } 94 | 95 | 96 | int main() { 97 | // Input: 8 3 10 1 6 14 4 7 13 98 | node* root = buildBST(); 99 | 100 | levelOrder(root); 101 | preOrder(root); 102 | cout << endl; 103 | inorder(root); 104 | cout << endl; 105 | postOrder(root); 106 | cout << endl; 107 | 108 | return 0; 109 | } 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /L18-BST/2_PrintRangeElements: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L18-BST/2_PrintRangeElements -------------------------------------------------------------------------------- /L18-BST/2_PrintRangeElements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left, *right; 9 | node(int d) { 10 | data = d; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | node* insertInBST(node* root, int data) { 16 | if (!root) { 17 | root = new node(data); 18 | return root; 19 | } 20 | 21 | if (data < root->data) { 22 | root->left = insertInBST(root->left, data); 23 | } 24 | else { 25 | root->right = insertInBST(root->right, data); 26 | } 27 | return root; 28 | } 29 | 30 | node* buildBST() { 31 | 32 | node* root = NULL; 33 | 34 | 35 | int data; 36 | cin >> data; 37 | while (data != -1) { 38 | root = insertInBST(root, data); 39 | 40 | cin >> data; 41 | } 42 | 43 | return root; 44 | } 45 | 46 | 47 | void printRange(node* root, int k1, int k2) { 48 | if (!root) return; 49 | 50 | if (root->data > k2) { // only go to LST 51 | printRange(root->left, k1, k2); 52 | } 53 | else if (root->data < k1) { // only go to RST 54 | printRange(root->right, k1, k2); 55 | } 56 | else { 57 | printRange(root->left, k1, k2); // Recursion se k1, k2 range ke elements ko print 58 | // krwaya 59 | if (root->data >= k1 and root->data <= k2) { 60 | cout << root->data << " "; 61 | } 62 | printRange(root->right, k1, k2); 63 | } 64 | } 65 | 66 | 67 | int main() { 68 | // Input: 8 3 10 1 6 14 4 7 13 -1 69 | node* root = buildBST(); 70 | 71 | printRange(root, 3, 10); 72 | cout << endl; 73 | 74 | 75 | return 0; 76 | } 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /L18-BST/3_SearchBST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L18-BST/3_SearchBST -------------------------------------------------------------------------------- /L18-BST/3_SearchBST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class node { 6 | public: 7 | int data; 8 | node* left, *right; 9 | node(int d) { 10 | data = d; 11 | left = right = NULL; 12 | } 13 | }; 14 | 15 | node* insertInBST(node* root, int data) { 16 | if (!root) { 17 | root = new node(data); 18 | return root; 19 | } 20 | 21 | if (data < root->data) { 22 | root->left = insertInBST(root->left, data); 23 | } 24 | else { 25 | root->right = insertInBST(root->right, data); 26 | } 27 | return root; 28 | } 29 | 30 | node* buildBST() { 31 | 32 | node* root = NULL; 33 | 34 | 35 | int data; 36 | cin >> data; 37 | while (data != -1) { 38 | root = insertInBST(root, data); 39 | 40 | cin >> data; 41 | } 42 | 43 | return root; 44 | } 45 | 46 | node* search(node* root, int key) { 47 | if (!root) return NULL; 48 | 49 | if (root->data == key) return root; 50 | else if (root -> data < key) { 51 | node*ans = search(root->right, key); // RST se pucha ki key hai ya nhi batao 52 | return ans; 53 | } 54 | else { 55 | return search(root->left, key); 56 | } 57 | } 58 | 59 | int main() { 60 | // Input: 8 3 10 1 6 14 4 7 13 -1 61 | node* root = buildBST(); 62 | 63 | node* ans = search(root, 100); 64 | if (ans) cout << ans->data << " "; 65 | else cout << "Not Found\n"; 66 | 67 | 68 | return 0; 69 | } 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /L18-BST/4_isBST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L18-BST/4_isBST -------------------------------------------------------------------------------- /L18-BST/4_isBST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* insertInBST(node* root, int data) { 15 | if (!root) { 16 | root = new node(data); 17 | return root; 18 | } 19 | 20 | if (data < root->data) { 21 | root->left = insertInBST(root->left, data); 22 | } 23 | else { 24 | root->right = insertInBST(root->right, data); 25 | } 26 | return root; 27 | } 28 | 29 | node* buildBST() { 30 | 31 | node* root = NULL; 32 | 33 | 34 | int data; 35 | cin >> data; 36 | while (data != -1) { 37 | root = insertInBST(root, data); 38 | 39 | cin >> data; 40 | } 41 | 42 | return root; 43 | } 44 | 45 | 46 | int minTree(node*root) { 47 | if (!root) return INT_MAX; 48 | 49 | int left = minTree(root->left); 50 | int right = minTree(root->right); 51 | 52 | return min(root->data, min(left, right)); 53 | } 54 | 55 | int maxTree(node*root) { 56 | if (!root) return INT_MIN; 57 | 58 | int left = maxTree(root->left); // LST ka max value bata 59 | int right = maxTree(root->right);// RST ka max value bata 60 | 61 | return max(root->data, max(left, right)); 62 | } 63 | 64 | bool isBST(node* root) { 65 | if (!root) return true; 66 | 67 | int leftMax = maxTree(root->left); 68 | int rightMin = minTree(root->right); 69 | 70 | if (root->data >= leftMax and root->data <= rightMin and isBST(root->left) and 71 | isBST(root->right)) return true; 72 | else return false; 73 | } 74 | 75 | int main() { 76 | 77 | node* root = buildBST(); 78 | if (isBST(root)) cout << "BST\n"; 79 | else cout << "Not a BST\n"; 80 | 81 | 82 | return 0; 83 | } 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /L18-BST/5_isBSTFast: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L18-BST/5_isBSTFast -------------------------------------------------------------------------------- /L18-BST/5_isBSTFast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* insertInBST(node* root, int data) { 15 | if (!root) { 16 | root = new node(data); 17 | return root; 18 | } 19 | 20 | if (data < root->data) { 21 | root->left = insertInBST(root->left, data); 22 | } 23 | else { 24 | root->right = insertInBST(root->right, data); 25 | } 26 | return root; 27 | } 28 | 29 | node* buildBST() { 30 | 31 | node* root = NULL; 32 | 33 | int data; 34 | cin >> data; 35 | while (data != -1) { 36 | root = insertInBST(root, data); 37 | 38 | cin >> data; 39 | } 40 | 41 | return root; 42 | } 43 | 44 | bool isBST(node* root, int mi = INT_MIN, int mx = INT_MAX) { 45 | if (!root) return true; 46 | 47 | if (root->data >= mi and root->data <= mx and isBST(root->left, mi, root->data) and 48 | isBST(root->right, root->data, mx)) return true; 49 | else return false; 50 | } 51 | 52 | int main() { 53 | 54 | node* root = buildBST(); 55 | if (isBST(root)) cout << "BST\n"; 56 | else cout << "Not a BST\n"; 57 | 58 | 59 | return 0; 60 | } 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /L18-BST/CBNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | bool isCBNo(long long n) { 8 | if (n == 0 || n == 1) { 9 | return false; 10 | } 11 | 12 | long long arr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; 13 | 14 | for (long long val : arr) { 15 | if (n == val) { 16 | return true; 17 | } 18 | } 19 | 20 | for (long long val : arr) { 21 | if (n % val == 0) { 22 | return false; 23 | } 24 | } 25 | 26 | return true; 27 | } 28 | 29 | bool isValid(vector& visited, int start, int end) { 30 | for (int i = start; i < end; i++) { 31 | if (visited[i]) { 32 | return false; 33 | } 34 | } 35 | return true; 36 | } 37 | 38 | int main() { 39 | int n; 40 | cin >> n; 41 | 42 | string str; 43 | cin >> str; 44 | 45 | int count = 0; 46 | vector visited(str.length(), false); 47 | 48 | for (int len = 1; len <= str.length(); len++) { 49 | for (int si = 0; si <= str.length() - len; si++) { 50 | int ei = si + len; 51 | string ss = str.substr(si, len); 52 | 53 | if (isCBNo(stoll(ss)) && isValid(visited, si, ei)) { 54 | count++; 55 | 56 | for (int i = si; i < ei; i++) { 57 | visited[i] = true; 58 | } 59 | } 60 | } 61 | } 62 | 63 | cout << count << endl; 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /L18-BST/input.txt: -------------------------------------------------------------------------------- 1 | 8 3 10 1 6 14 4 7 13 -1 -------------------------------------------------------------------------------- /L18-BST/random.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | private class Node { 5 | 6 | int data; 7 | Node left; 8 | Node right; 9 | 10 | Node(int data, Node left, Node right) { 11 | this.data = data; 12 | this.left = left; 13 | this.right = right; 14 | } 15 | } 16 | 17 | private Node root; 18 | private int size; 19 | private int counter; 20 | 21 | public Main(int... sa) { 22 | this.root = this.construct(sa, 0, sa.length - 1); 23 | } 24 | 25 | private Node construct(int[] sa, int lo, int hi) { 26 | if (lo > hi) { 27 | return null; 28 | } 29 | int mid = (lo + hi) / 2; 30 | Node rv = new Node(sa[mid], null, null); 31 | this.size++; 32 | rv.left = this.construct(sa, lo, mid - 1); 33 | rv.right = this.construct(sa, mid + 1, hi); 34 | return rv; 35 | } 36 | 37 | public int findSubtrees(int min, int max) { 38 | findSubtrees(this.root, min, max); 39 | 40 | return counter; 41 | } 42 | 43 | private boolean findSubtrees(Node node, int min, int max) { 44 | if (node == null) { 45 | return true; 46 | } 47 | boolean l = (node.left != null) ? (findSubtrees(node.left, min, max)) : true; 48 | boolean r = (node.right != null) ? (findSubtrees(node.right, min, max)) : true; 49 | 50 | if (l & r & currentIsInRange(node.data, min, max)) { 51 | ++counter; 52 | return true; 53 | } 54 | return false; 55 | } 56 | 57 | private boolean currentIsInRange(int key, int min, int max) { 58 | // TODO Auto-generated method stub 59 | return key >= min && key <= max; 60 | } 61 | 62 | public static void main(String[] args) { 63 | Scanner scn = new Scanner(System.in); 64 | int n = scn.nextInt(); 65 | int[] arr = new int[n]; 66 | for (int i = 0; i < n; i++) { 67 | arr[i] = scn.nextInt(); 68 | } 69 | // int s = scn.nextInt(); 70 | Main b = new Main(arr); 71 | int l = scn.nextInt(); 72 | int r = scn.nextInt(); 73 | System.out.println(b.findSubtrees(l, r)); 74 | // b.printNodesSumToS(s); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /L19-TreesAndHeap/1_BSTtoLL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L19-TreesAndHeap/1_BSTtoLL -------------------------------------------------------------------------------- /L19-TreesAndHeap/1_BSTtoLL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node { 5 | public: 6 | int data; 7 | node* left, *right; 8 | node(int d) { 9 | data = d; 10 | left = right = NULL; 11 | } 12 | }; 13 | 14 | node* insertInBST(node* root, int data) { 15 | if (!root) { 16 | root = new node(data); 17 | return root; 18 | } 19 | 20 | if (data < root->data) { 21 | root->left = insertInBST(root->left, data); 22 | } 23 | else { 24 | root->right = insertInBST(root->right, data); 25 | } 26 | return root; 27 | } 28 | 29 | node* buildBST() { 30 | 31 | node* root = NULL; 32 | 33 | int data; 34 | cin >> data; 35 | while (data != -1) { 36 | root = insertInBST(root, data); 37 | 38 | cin >> data; 39 | } 40 | 41 | return root; 42 | } 43 | 44 | class LinkedList { 45 | public: 46 | node* head, *tail; 47 | }; 48 | 49 | LinkedList bstToLL(node* root) { 50 | if (!root) { 51 | return {NULL, NULL}; 52 | } 53 | 54 | // Recursive case 55 | if (root->left and root->right) { // LST and RST both exists 56 | LinkedList left = bstToLL(root->left); // LST to LL FROM RECURSION 57 | LinkedList right = bstToLL(root->right); // RST to LL FROM RECURSION 58 | left.tail->right = root; 59 | root->right = right.head; 60 | 61 | return {left.head, right.tail}; 62 | } 63 | else if (root->left and !root->right) { // LST exist and RST doesn't exist 64 | LinkedList left = bstToLL(root->left); // LST to LL FROM RECURSION 65 | left.tail->right = root; 66 | return {left.head, root}; 67 | } 68 | else if (!root->left and root->right) { // LST doesn't exist and RST exist 69 | LinkedList right = bstToLL(root->right); // RST to LL FROM RECURSION 70 | root->right = right.head; 71 | 72 | return {root, right.tail}; 73 | } 74 | else { 75 | // LinkedList l; 76 | // l.head = l.tail = root; 77 | return {root, root}; 78 | } 79 | } 80 | 81 | int main() { 82 | 83 | node* root = buildBST(); 84 | 85 | LinkedList ans = bstToLL(root); 86 | 87 | node* head = ans.head; 88 | while (head) { 89 | cout << head->data << " --> "; 90 | head = head->right; 91 | } cout << "NULL\n"; 92 | 93 | return 0; 94 | } 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /L19-TreesAndHeap/2_kthSmallestElement.cpp: -------------------------------------------------------------------------------- 1 | #define node TreeNode 2 | class Solution { 3 | public: 4 | int cnt = 0; 5 | int ans; 6 | void solve(node* root, int k) { 7 | if (!root) return; 8 | 9 | solve(root->left, k); 10 | cnt++; 11 | if (cnt == k) { 12 | ans = root->val; 13 | return; 14 | } 15 | solve(root->right, k); 16 | } 17 | 18 | int kthSmallest(node* root, int k) { 19 | solve(root, k); 20 | return ans; 21 | } 22 | }; -------------------------------------------------------------------------------- /L19-TreesAndHeap/3_uniqueBST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTrees(int n) { 4 | if (n == 1 || n == 0) return 1; 5 | 6 | int ans = 0; 7 | for (int i = 1; i <= n; ++i) 8 | { 9 | ans += numTrees(i - 1) * numTrees(n - i); 10 | } 11 | return ans; 12 | } 13 | }; -------------------------------------------------------------------------------- /L19-TreesAndHeap/4_NcR: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L19-TreesAndHeap/4_NcR -------------------------------------------------------------------------------- /L19-TreesAndHeap/4_NcR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fact(int n) { 5 | int ans = 1; 6 | for (int i = 1; i <= n; ++i) 7 | { 8 | ans *= i; 9 | } 10 | return ans; 11 | } 12 | 13 | int ncr(int n, int r) { 14 | int fact_n = fact(n); 15 | int fact_r = fact(r); 16 | int fact_nr = fact(n - r); 17 | 18 | return fact_n / (fact_r * fact_nr); 19 | } 20 | 21 | int main() { 22 | 23 | cout << ncr(5, 2) << endl; 24 | 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /L19-TreesAndHeap/input.txt: -------------------------------------------------------------------------------- 1 | 8 3 10 1 6 14 4 7 13 -1 -------------------------------------------------------------------------------- /L2-Arrays/1_Basics: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L2-Arrays/1_Basics -------------------------------------------------------------------------------- /L2-Arrays/1_Basics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {1, 2, 3, 4, 5}; 7 | int n = sizeof(a) / sizeof(int); 8 | 9 | int key = 41; 10 | int i; 11 | 12 | for (i = 0; i < n; ++i) 13 | { 14 | if (a[i] == key) { 15 | cout << "Found at index: " << i << endl; 16 | break; 17 | } 18 | } 19 | 20 | if (i == n) cout << "Key not found\n"; 21 | 22 | 23 | 24 | /* 25 | // Error dega that break cannot be used inside if 26 | if (n > 0) { 27 | break; 28 | } 29 | 30 | */ 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /L2-Arrays/2_Continue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L2-Arrays/2_Continue -------------------------------------------------------------------------------- /L2-Arrays/2_Continue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int i = 0; 7 | int n; 8 | cin >> n; 9 | 10 | while (i < n) { 11 | 12 | cout << i << " "; 13 | 14 | if (i == 3) { 15 | i++; 16 | continue; 17 | } 18 | 19 | i++; 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /L2-Arrays/3_SubArrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L2-Arrays/3_SubArrays -------------------------------------------------------------------------------- /L2-Arrays/3_SubArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = {1, 2, 3, 4}; 8 | int n = sizeof(a) / sizeof(int); 9 | int sum; 10 | int ans = INT_MIN; 11 | 12 | for (int i = 0; i < n; ++i) 13 | { 14 | for (int j = i; j < n; ++j) 15 | { 16 | sum = 0; 17 | // cout << i << ", " << j << endl; 18 | for (int k = i; k <= j; ++k) 19 | { 20 | // cout << a[k] << " "; 21 | sum += a[k]; 22 | } 23 | 24 | ans = max(ans, sum); 25 | 26 | // cout << ", sum :" << sum << endl; 27 | 28 | } 29 | } 30 | 31 | cout << "max sum: " << ans << endl; 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /L2-Arrays/4_PrintSubarrayWithMaxSum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L2-Arrays/4_PrintSubarrayWithMaxSum -------------------------------------------------------------------------------- /L2-Arrays/4_PrintSubarrayWithMaxSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = {1, 2, -5, 3, 8, -2, 6}; 8 | int n = sizeof(a) / sizeof(int); 9 | int sum; 10 | int ans = INT_MIN; 11 | int si, sj; 12 | 13 | for (int i = 0; i < n; ++i) 14 | { 15 | for (int j = i; j < n; ++j) 16 | { 17 | sum = 0; 18 | for (int k = i; k <= j; ++k) 19 | { 20 | sum += a[k]; 21 | } 22 | 23 | if (sum > ans) { 24 | ans = sum; 25 | si = i; // Store the starting index of maxsum subarray 26 | sj = j; // Store the ending index of maxsum subarray 27 | } 28 | 29 | } 30 | } 31 | 32 | 33 | cout << "Sum : " << ans << endl; 34 | for (int i = si; i <= sj; ++i) 35 | { 36 | cout << a[i] << " "; 37 | } 38 | cout << endl; 39 | 40 | 41 | return 0; 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /L2-Arrays/5_MaxSumTwoArrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L2-Arrays/5_MaxSumTwoArrays -------------------------------------------------------------------------------- /L2-Arrays/5_MaxSumTwoArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = {1, 2, -5, 3, 8, -2, 6}; 8 | int n = sizeof(a) / sizeof(int); 9 | int sum; 10 | int ans = INT_MIN; 11 | int si, sj; 12 | int ps[100] = {0}; 13 | 14 | // Find the prefix sum first to find range sum 15 | ps[0] = a[0]; 16 | for (int i = 1; i < n; ++i) 17 | { 18 | ps[i] = ps[i - 1] + a[i]; 19 | // ps[i]: [0,i] range sum 20 | // ps[i-1]: [0,i-1] range sum 21 | } 22 | 23 | // Max Sum Algo 24 | 25 | for (int i = 0; i < n; ++i) 26 | { 27 | for (int j = i; j < n; ++j) 28 | { 29 | /* 30 | if (i - 1 >= 0) { 31 | sum = ps[j] - ps[i - 1]; 32 | } 33 | else sum = ps[j]; 34 | */ 35 | sum = ps[j] - ( i - 1 >= 0 ? ps[i - 1] : 0 ); 36 | 37 | if (sum > ans) { 38 | ans = sum; 39 | si = i; // Store the starting index of maxsum subarray 40 | sj = j; // Store the ending index of maxsum subarray 41 | } 42 | 43 | } 44 | } 45 | 46 | cout << "Sum : " << ans << endl; 47 | for (int i = si; i <= sj; ++i) 48 | { 49 | cout << a[i] << " "; 50 | } 51 | cout << endl; 52 | 53 | 54 | return 0; 55 | } 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /L2-Arrays/6_Kadanes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L2-Arrays/6_Kadanes -------------------------------------------------------------------------------- /L2-Arrays/6_Kadanes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a[] = { -1, -2, -5, 3, -8, -2, -6}; 8 | int n = sizeof(a) / sizeof(int); 9 | int sum = 0; 10 | int ans = INT_MIN; 11 | 12 | 13 | bool isPositivePresent = false; 14 | for (int i = 0; i < n; ++i) 15 | { 16 | if (a[i] >= 0) { 17 | isPositivePresent = true; 18 | } 19 | 20 | ans = max(ans, a[i]); // -ve elements mei ans un sabka max hoga 21 | } 22 | 23 | 24 | if (isPositivePresent == true) { 25 | // Max Sum Algo 26 | for (int i = 0; i < n; ++i) 27 | { 28 | sum += a[i]; // Elements ko add karte jaao 29 | if (sum < 0) sum = 0; // Sum agar -ve ho toh make it 0 30 | 31 | ans = max(ans, sum); // ans ke andar max lete jaao 32 | } 33 | } 34 | 35 | cout << "Sum : " << ans << 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 | -------------------------------------------------------------------------------- /L3-Arrays/1_ProductOfArrayExceptSelf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L3-Arrays/1_ProductOfArrayExceptSelf -------------------------------------------------------------------------------- /L3-Arrays/1_ProductOfArrayExceptSelf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {1, 2, 3, 4, 5}; 7 | int n = sizeof(a) / sizeof(int); // 5 8 | 9 | int left[100]; 10 | int right[100]; 11 | 12 | // Left side ke products ko calculate karte hai for every index i 13 | // left[i] : a[0]*a[1]*a[2]* . . . . * a[i-1] 14 | 15 | left[0] = 1;// because 0th index se pehle koi element hai hi nhi 16 | int ans = a[0]; 17 | for (int i = 1; i < n; ++i) 18 | { 19 | left[i] = ans; 20 | ans *= a[i]; 21 | } 22 | 23 | // Right side ke products ko calculate karte hai for every index i 24 | // right[i] : a[i+1]*a[i+2]* . . . . * a[n-1] 25 | 26 | right[n - 1] = 1; 27 | ans = a[n - 1]; 28 | for (int i = n - 2; i >= 0; --i) 29 | { 30 | right[i] = ans; 31 | ans *= a[i]; 32 | } 33 | 34 | // Finding the final res[] 35 | int res[100]; 36 | for (int i = 0; i < n; ++i) 37 | { 38 | // left[i] = a[0]*a[1]*a[2]* . . . . * a[i-1] 39 | // right[i] : a[i+1]*a[i+2]* . . . . * a[n-1] 40 | res[i] = left[i] * right[i]; 41 | } 42 | 43 | 44 | for (int i = 0; i < n; ++i) 45 | { 46 | cout << res[i] << " "; 47 | } 48 | cout << endl; 49 | 50 | return 0; 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /L3-Arrays/2_ProductOfArrayBestApproach: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L3-Arrays/2_ProductOfArrayBestApproach -------------------------------------------------------------------------------- /L3-Arrays/2_ProductOfArrayBestApproach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {1, 2, 3, 4, 5}; 7 | int n = sizeof(a) / sizeof(int); // 5 8 | 9 | int left[100]; 10 | int right[100]; 11 | int res[100]; 12 | for (int i = 0; i < n; ++i) 13 | { 14 | res[i] = 1; 15 | } 16 | 17 | // res ke andar hi products ko calculate karte hai for every index i 18 | // res[i] : a[0]*a[1]*a[2]* . . . . * a[i-1] 19 | 20 | res[0] = 1;// because 0th index se pehle koi element hai hi nhi 21 | int ans = a[0]; 22 | for (int i = 1; i < n; ++i) 23 | { 24 | res[i] = ans; 25 | ans *= a[i]; 26 | } 27 | 28 | // Right side ke products ko calculate karte hai for every index i and multiply kardo 29 | // res ke array ke andar hi 30 | // right[i] : a[i+1]*a[i+2]* . . . . * a[n-1] 31 | 32 | // right[n - 1] = 1; 33 | ans = a[n - 1]; 34 | for (int i = n - 2; i >= 0; --i) 35 | { 36 | res[i] *= ans; 37 | ans *= a[i]; 38 | } 39 | 40 | for (int i = 0; i < n; ++i) 41 | { 42 | cout << res[i] << " "; 43 | } 44 | cout << endl; 45 | 46 | return 0; 47 | } 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /L3-Arrays/3_RainWaterTrapping: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L3-Arrays/3_RainWaterTrapping -------------------------------------------------------------------------------- /L3-Arrays/3_RainWaterTrapping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {0, 2, 0}; 7 | int n = sizeof(a) / sizeof(int); 8 | 9 | int leftMax[100], rightMax[100]; 10 | 11 | int ans = a[0]; 12 | leftMax[0] = 0; 13 | for (int i = 1; i < n; ++i) 14 | { 15 | leftMax[i] = ans; 16 | ans = max(ans, a[i]); 17 | } 18 | 19 | ans = a[n - 1]; 20 | rightMax[n - 1] = 0; 21 | for (int i = n - 2; i >= 0; --i) 22 | { 23 | rightMax[i] = ans; 24 | ans = max(ans, a[i]); 25 | } 26 | 27 | int totalWater = 0; 28 | for (int i = 0; i < n; ++i) 29 | { 30 | int minLR = min(leftMax[i], rightMax[i]); 31 | if (minLR - a[i] > 0) { 32 | totalWater += (minLR - a[i]); 33 | } 34 | } 35 | 36 | cout << totalWater << endl; 37 | 38 | return 0; 39 | } 40 | 41 | 42 | 43 | /* 44 | // Leetcode Submission: 45 | 46 | class Solution { 47 | public: 48 | int trap(vector& a) { 49 | int leftMax[50001], rightMax[50001]; 50 | int n = a.size(); 51 | int ans = a[0]; 52 | leftMax[0] = 0; 53 | for (int i = 1; i < n; ++i) 54 | { 55 | leftMax[i] = ans; 56 | ans = max(ans, a[i]); 57 | } 58 | 59 | ans = a[n - 1]; 60 | rightMax[n-1] = 0; 61 | for (int i = n - 2; i >= 0; --i) 62 | { 63 | rightMax[i] = ans; 64 | ans = max(ans, a[i]); 65 | } 66 | 67 | int totalWater = 0; 68 | for (int i = 0; i < n; ++i) 69 | { 70 | int minLR = min(leftMax[i], rightMax[i]); 71 | if (minLR - a[i] > 0) { 72 | totalWater += (minLR - a[i]); 73 | } 74 | } 75 | 76 | return totalWater; 77 | } 78 | }; 79 | */ 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /L3-Arrays/4_3Sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L3-Arrays/4_3Sum -------------------------------------------------------------------------------- /L3-Arrays/4_3Sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = { -1, 0, 1, 2, -1, -4}; 7 | int n = sizeof(a) / sizeof(int); 8 | 9 | 10 | sort(a, a + n); // Sorting using inbuilt function 11 | 12 | for (int i = 0; i < n - 2; ++i) 13 | { 14 | if (a[i] > 0) break; 15 | 16 | // Ek baari jo fix ho gaya usse dobara nhi lena 17 | if (i > 0 and a[i] == a[i - 1]) continue; 18 | 19 | // Ek element ko fix kardo: a[i] 20 | int j = i + 1; 21 | int k = n - 1; 22 | while (j < k) { 23 | if (a[j] + a[k] == -a[i]) { 24 | cout << a[i] << ", " << a[j] << ", " << a[k] << endl; 25 | // Ek baari triplet mill gaya toh a[j] and a[k] dono ko hatado 26 | j++; 27 | k--; 28 | // Agar humne jo j ko lia hai already whi same j dobara aaya 29 | // toh ignore kardo un saare j's ko 30 | while (j < k and a[j] == a[j - 1]) j++; 31 | } 32 | else if (a[j] + a[k] < -a[i])j++; 33 | else k--; 34 | } 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /L3-Arrays/5_Sorting: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L3-Arrays/5_Sorting -------------------------------------------------------------------------------- /L3-Arrays/5_Sorting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // #include // For greater() 4 | using namespace std; 5 | 6 | bool meraComparator(int a, int b) { 7 | return a < b; 8 | } 9 | 10 | int main() { 11 | 12 | int a[] = {6, 5, 9, 2, 3, 0, 1}; 13 | int n = sizeof(a) / sizeof(int); 14 | 15 | // sort(begining_address, ending_address); 16 | // sort(a, a + n, greater()); 17 | sort(a, a + n, meraComparator); 18 | // sort(a, a + n, [&](int a, int b) { 19 | // return a > b; 20 | // }); 21 | 22 | for (int i = 0; i < n; ++i) 23 | { 24 | cout << a[i] << " "; 25 | } 26 | 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /L3-Arrays/6_PatchingArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L3-Arrays/6_PatchingArray -------------------------------------------------------------------------------- /L3-Arrays/6_PatchingArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[] = {1, 5, 10}; 7 | int a_size = sizeof(a) / sizeof(int); 8 | int i = 0; 9 | int N = 20; 10 | int patchingCount = 0; 11 | 12 | int nextNumberNeeded = 1; 13 | int weCanGenerateTill = 0; 14 | ` 15 | while (weCanGenerateTill < N) { 16 | // Jab tak hum nextNumberNeeded ko jaha tak generateKar skte usse upr nhi krte 17 | // there is no point 18 | if (weCanGenerateTill >= nextNumberNeeded) nextNumberNeeded = weCanGenerateTill + 1; 19 | else { 20 | // Patching nhi hogi 21 | if (i < a_size and nextNumberNeeded >= a[i]) { 22 | weCanGenerateTill += a[i]; 23 | i++; 24 | } 25 | else { // Patching hogi 26 | patchingCount ++; 27 | weCanGenerateTill += nextNumberNeeded; 28 | } 29 | } 30 | } 31 | 32 | cout << patchingCount << endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /L4-Arrays/1_DayAtBeach: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L4-Arrays/1_DayAtBeach -------------------------------------------------------------------------------- /L4-Arrays/1_DayAtBeach.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int a[100005]; 7 | int n; 8 | 9 | cin >> n; 10 | for (int i = 0; i < n; ++i) 11 | { 12 | cin >> a[i]; 13 | } 14 | 15 | int leftMax[100005], rightMin[100005]; 16 | 17 | leftMax[0] = INT_MAX; 18 | int ans = a[0]; 19 | for (int i = 1; i < n; ++i) 20 | { 21 | leftMax[i] = ans; 22 | ans = max(ans, a[i]); 23 | } 24 | 25 | ans = INT_MAX; 26 | for (int i = n - 1; i >= 0; --i) 27 | { 28 | ans = min(ans, a[i]); 29 | rightMin[i] = ans; 30 | } 31 | 32 | int res = 1; 33 | for (int i = 1; i < n; ++i) 34 | { 35 | if (leftMax[i] <= rightMin[i]) res++; 36 | } 37 | cout << res << endl; 38 | return 0; 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /L4-Arrays/2_BinarySearch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L4-Arrays/2_BinarySearch -------------------------------------------------------------------------------- /L4-Arrays/2_BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(int *a, int n, int key) { 5 | int s = 0, e = n - 1; 6 | 7 | while (s <= e) { 8 | int mid = (s + e) / 2; 9 | 10 | if (a[mid] == key) return mid; 11 | else if (key > a[mid]) s = mid + 1; 12 | else e = mid - 1; 13 | } 14 | 15 | return -1; // Key is not found 16 | } 17 | 18 | int main() { 19 | 20 | 21 | int a[] = {1, 2, 3, 4, 5}; 22 | int n = sizeof(a) / sizeof(int); 23 | 24 | cout << binarySearch(a, n, 15) << endl; 25 | 26 | 27 | 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /L4-Arrays/3_MedianOfSorted: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L4-Arrays/3_MedianOfSorted -------------------------------------------------------------------------------- /L4-Arrays/3_MedianOfSorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double median(int *a, int *b, int n, int m) { 5 | 6 | if (n > m) return median(b, a, m, n); 7 | 8 | int s = 0, e = n; 9 | 10 | while (s <= e) { 11 | 12 | int mid = (s + e) / 2; // Parition in a[]: Yaha tak left mei jaenge elements a[] 13 | int mid1 = (n + m + 1) / 2 - mid; // Parition in b[]:Yaha tak left mei jaenge elements b[] 14 | 15 | int leftMaxa = (mid - 1 >= 0) ? a[mid - 1] : INT_MIN ; 16 | int leftMaxb = (mid1 - 1 >= 0) ? b[mid1 - 1] : INT_MIN; 17 | int rightMina = (mid < n) ? a[mid] : INT_MAX; 18 | int rightMinb = (mid1 < m) ? b[mid1] : INT_MAX; 19 | 20 | if (leftMaxa < rightMinb and leftMaxb < rightMina) { 21 | if ((n + m) % 2 == 1) return max(leftMaxa, leftMaxb); 22 | else return (max(leftMaxa, leftMaxb) + min(rightMina, rightMinb)) / 2.0; 23 | } 24 | else if (leftMaxa > rightMinb) e = mid - 1; 25 | else s = mid + 1; 26 | } 27 | return 0; 28 | } 29 | 30 | 31 | int main() { 32 | 33 | int a[] = {1, 4, 7, 9, 11}; 34 | int b[] = {2, 3, 5, 6, 8, 10}; 35 | 36 | int n = sizeof(a) / sizeof(int); 37 | int m = sizeof(b) / sizeof(int); 38 | 39 | cout << median(a, b, n, m) << endl; 40 | 41 | return 0; 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /L4-Arrays/3_SearchRotatedSorted: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L4-Arrays/3_SearchRotatedSorted -------------------------------------------------------------------------------- /L4-Arrays/3_SearchRotatedSorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int searchRotated(int *a, int n, int key) { 5 | int s = 0, e = n - 1; 6 | 7 | while (s <= e) { 8 | int mid = (s + e) / 2; 9 | 10 | if (a[mid] == key) return mid; 11 | else if (a[s] <= a[mid]) { 12 | if (key >= a[s] and key <= a[mid]) e = mid - 1; 13 | else s = mid + 1; 14 | } 15 | else { 16 | if (key >= a[mid] and key <= a[e]) s = mid + 1; 17 | else e = mid - 1; 18 | } 19 | } 20 | 21 | return -1; 22 | } 23 | 24 | int main() { 25 | int a[] = {7, 8, 10, 11, 15, 0, 1, 3, 4, 5}; 26 | int n = sizeof(a) / sizeof(int); 27 | 28 | cout << searchRotated(a, n, 15) << endl; 29 | 30 | 31 | 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /L5-BinarySearch/1_Test. pp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L5-BinarySearch/1_Test. pp -------------------------------------------------------------------------------- /L5-BinarySearch/1_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 | -------------------------------------------------------------------------------- /L5-BinarySearch/2_AggressiveCows: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L5-BinarySearch/2_AggressiveCows -------------------------------------------------------------------------------- /L5-BinarySearch/2_AggressiveCows.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int a[100005]; 5 | int n, totalCows; 6 | 7 | bool kyaCowsPlaceHoPaai(int d) { 8 | int cowsPlaced = 1; 9 | int pc = a[0]; 10 | for (int i = 1; i < n; ++i) 11 | { 12 | if (a[i] - pc >= d) { 13 | cowsPlaced++; 14 | pc = a[i]; 15 | 16 | if (cowsPlaced == totalCows) return true; 17 | } 18 | } 19 | 20 | return false; 21 | } 22 | 23 | int aggressiveCows() { 24 | // 1. Find the search space 25 | int s = 0, e = a[n - 1] - a[0]; 26 | int ans = 0; 27 | while (s <= e) { 28 | int mid = s + (e - s) / 2; 29 | if (kyaCowsPlaceHoPaai(mid)) { 30 | ans = mid; 31 | s = mid + 1; 32 | } 33 | else { 34 | e = mid - 1; 35 | } 36 | } 37 | 38 | return ans; 39 | } 40 | 41 | int main() { 42 | int t; 43 | cin >> t; 44 | 45 | while (t--) { 46 | 47 | cin >> n >> totalCows; 48 | 49 | for (int i = 0; i < n; ++i) 50 | { 51 | cin >> a[i]; 52 | } 53 | 54 | sort(a, a + n); 55 | 56 | cout << aggressiveCows() << endl; 57 | } 58 | 59 | 60 | return 0; 61 | } 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /L5-BinarySearch/3_bookAllocation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L5-BinarySearch/3_bookAllocation -------------------------------------------------------------------------------- /L5-BinarySearch/3_bookAllocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int n, totalStudents; 4 | int books[100005]; 5 | 6 | bool kyaBooksDistributeHoPaai(int allowedPages) { 7 | int pages = 0; 8 | int studentsCnt = 1; 9 | 10 | for (int i = 0; i < n; ++i) 11 | { 12 | if (pages + books[i] <= allowedPages) { 13 | pages += books[i]; 14 | } 15 | else { 16 | studentsCnt++; 17 | pages = books[i]; 18 | } 19 | } 20 | 21 | if (studentsCnt <= totalStudents) return true; 22 | else return false; 23 | } 24 | 25 | int bookAllocation() { 26 | // Finding search space 27 | int s = 0, e = 0; 28 | for (int i = 0; i < n; ++i) 29 | { 30 | s = max(s, books[i]); 31 | e += books[i]; 32 | } 33 | int ans = 0; 34 | 35 | while (s <= e) { 36 | int mid = s + (e - s) / 2; 37 | 38 | if (kyaBooksDistributeHoPaai(mid)) { 39 | ans = mid; 40 | e = mid - 1; 41 | } 42 | else { 43 | s = mid + 1; 44 | } 45 | } 46 | 47 | return ans; 48 | } 49 | 50 | int main() { 51 | 52 | int t; cin >> t; 53 | while (t--) { 54 | cin >> n >> totalStudents; 55 | for (int i = 0; i < n; ++i) 56 | { 57 | cin >> books[i]; 58 | } 59 | 60 | cout << bookAllocation() << endl; 61 | } 62 | 63 | 64 | return 0; 65 | } 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /L5-BinarySearch/4_MurthalParantha: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L5-BinarySearch/4_MurthalParantha -------------------------------------------------------------------------------- /L5-BinarySearch/4_MurthalParantha.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int requiredParanthas; 5 | int n; 6 | int cook[100005]; 7 | 8 | bool kyaSaareBanPaye(int timeAvailable) { 9 | int paranthasCooked = 0; 10 | 11 | for (int i = 0; i < n; ++i) 12 | { 13 | int r = cook[i]; 14 | int timeTaken = 0; 15 | int pranathaNumber = 1; 16 | while (timeTaken + pranathaNumber * r <= timeAvailable) { 17 | timeTaken += pranathaNumber * r; 18 | paranthasCooked++; 19 | 20 | pranathaNumber++; 21 | } 22 | } 23 | 24 | if (paranthasCooked >= requiredParanthas) return true; 25 | else return false; 26 | } 27 | 28 | int murthalParantha() { 29 | int s = 0, e = 1e9; 30 | int ans; 31 | while (s <= e) { 32 | int mid = s + (e - s) / 2; // Integer ki range overflow na krey 33 | 34 | if (kyaSaareBanPaye(mid)) { 35 | ans = mid; 36 | e = mid - 1; 37 | } 38 | else { 39 | s = mid + 1; 40 | } 41 | } 42 | 43 | return ans; 44 | } 45 | 46 | int main() { 47 | 48 | cin >> requiredParanthas; 49 | cin >> n; 50 | 51 | for (int i = 0; i < n; ++i) 52 | cin >> cook[i]; 53 | 54 | cout << murthalParantha() << endl; 55 | 56 | 57 | return 0; 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/1_PashaAndStrings: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/1_PashaAndStrings -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/1_PashaAndStrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int cnt[100005] = {0}; 4 | 5 | int main() { 6 | 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | string s; 12 | cin >> s; 13 | int len = s.size(); 14 | int m; 15 | cin >> m; 16 | 17 | for (int i = 0; i < m; ++i) 18 | { 19 | int ai; cin >> ai; 20 | cnt[ai - 1]++; 21 | } 22 | 23 | for (int i = 1; i < len / 2 ; ++i) 24 | { 25 | cnt[i] += cnt[i - 1]; 26 | } 27 | 28 | for (int i = 0; i < len / 2 ; ++i) 29 | { 30 | if (cnt[i] % 2) { 31 | swap(s[i], s[len - i - 1]); 32 | } 33 | } 34 | 35 | cout << s << endl; 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/2_2DArrayBasics: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/2_2DArrayBasics -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/2_2DArrayBasics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | #ifndef ONLINE_JUDGE 7 | freopen("input.txt", "r", stdin); 8 | freopen("output.txt", "w", stdout); 9 | #endif 10 | 11 | int a[100][100]; 12 | int n, m; 13 | // n: no of rows, m: no of cols 14 | cin >> n >> m; 15 | int no = 1; 16 | 17 | for (int i = 0; i < n; ++i) 18 | { 19 | for (int j = 0; j < m; ++j) 20 | { 21 | a[i][j] = no++; 22 | // no = no + 1; 23 | } 24 | } 25 | 26 | for (int i = 0; i < n; ++i) 27 | { 28 | for (int j = 0; j < m; ++j) 29 | { 30 | cout << a[i][j] << " "; 31 | } 32 | cout << endl; 33 | } 34 | 35 | return 0; 36 | } 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/3_Transpose: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/3_Transpose -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/3_Transpose.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | #ifndef ONLINE_JUDGE 7 | freopen("input.txt", "r", stdin); 8 | freopen("output.txt", "w", stdout); 9 | #endif 10 | 11 | int a[100][100]; 12 | int n, m; 13 | // n: no of rows, m: no of cols 14 | cin >> n >> m; 15 | int no = 1; 16 | 17 | for (int i = 0; i < n; ++i) 18 | { 19 | for (int j = 0; j < m; ++j) 20 | { 21 | a[i][j] = no++; 22 | } 23 | } 24 | 25 | for (int i = 0; i < n - 1; ++i) 26 | { 27 | for (int j = i + 1; j < m; ++j) 28 | { 29 | swap(a[i][j], a[j][i]); 30 | } 31 | } 32 | 33 | for (int i = 0; i < n; ++i) 34 | { 35 | for (int j = 0; j < m; ++j) 36 | { 37 | cout << a[i][j] << " "; 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 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/4_WavePrint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/4_WavePrint -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/4_WavePrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | #ifndef ONLINE_JUDGE 6 | freopen("input.txt", "r", stdin); 7 | freopen("output.txt", "w", stdout); 8 | #endif 9 | int a[][4] = { 10 | {1, 2, 3, 4}, 11 | {5, 6, 7, 8}, 12 | {9, 10, 11, 12}, 13 | {13, 14, 15, 16}, 14 | }; 15 | int n = 4, m = 4; 16 | 17 | for (int j = 0; j < m; ++j) 18 | { 19 | if (j % 2 == 0) { 20 | for (int i = 0; i < n; ++i) 21 | { 22 | cout << a[i][j] << " "; 23 | } 24 | } 25 | else { 26 | for (int i = n - 1; i >= 0; --i) 27 | { 28 | cout << a[i][j] << " "; 29 | } 30 | } 31 | } 32 | 33 | cout << endl; 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/5_SpiralPrint: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/5_SpiralPrint -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/5_SpiralPrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void spiralPrint(int a[][100], int n, int m) { 5 | int sr = 0, sc = 0, er = n - 1, ec = m - 1; 6 | 7 | while (sr <= er and sc <= ec) { 8 | // 1. Print sr 9 | for (int c = sc; c <= ec; ++c) 10 | { 11 | cout << a[sr][c] << " "; 12 | } 13 | sr++; 14 | 15 | // 2. Print ec 16 | for (int r = sr; r <= er; ++r) 17 | { 18 | cout << a[r][ec] << " "; 19 | } 20 | ec--; 21 | 22 | // 3. Print er 23 | if (sr < er) { 24 | for (int c = ec; c >= sc; --c) 25 | { 26 | cout << a[er][c] << " "; 27 | } 28 | er--; 29 | } 30 | 31 | if (sc < ec) { 32 | // 4. Print sc 33 | for (int r = er; r >= sr; --r) 34 | { 35 | cout << a[r][sc] << " "; 36 | } 37 | sc++; 38 | } 39 | } 40 | } 41 | 42 | 43 | int main() { 44 | 45 | #ifndef ONLINE_JUDGE 46 | freopen("input.txt", "r", stdin); 47 | freopen("output.txt", "w", stdout); 48 | #endif 49 | 50 | int a[100][100]; 51 | int n, m; 52 | cin >> n >> m; 53 | int no = 1; 54 | 55 | for (int i = 0; i < n; ++i) 56 | { 57 | for (int j = 0; j < m; ++j) 58 | { 59 | a[i][j] = no++; 60 | } 61 | } 62 | 63 | for (int i = 0; i < n; ++i) 64 | { 65 | for (int j = 0; j < m; ++j) 66 | { 67 | cout << a[i][j] << " "; 68 | } 69 | cout << endl; 70 | } 71 | 72 | spiralPrint(a, n, m); 73 | 74 | 75 | 76 | return 0; 77 | } 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/6_CharacterArrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/6_CharacterArrays -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/6_CharacterArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | // #ifndef ONLINE_JUDGE 7 | // freopen("input.txt", "r", stdin); 8 | // freopen("output.txt", "w", stdout); 9 | // #endif 10 | 11 | // ['A','M','A','N','S'] 12 | // char name[6] = "AMANS"; 13 | char name[1000]; 14 | // cin >> name; 15 | // cin.getline(array_name, buckets, delimiter) 16 | // delimiter character by default is '\n' 17 | cin.getline(name, 100, '.'); 18 | 19 | 20 | cout << name << endl; 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/7_input: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/7_input -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/7_input.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void inputString(char a[], int n, char d = '\n') { 5 | char ch; 6 | int i = 0; 7 | ch = cin.get(); 8 | 9 | while (ch != d and i < n - 1) { 10 | 11 | // cout << "You entered: " << ch << ", i: " << i++ << '\n'; 12 | a[i++] = ch; 13 | 14 | ch = cin.get(); 15 | } 16 | 17 | // cout << i << "\n"; 18 | a[i] = '\0'; 19 | } 20 | 21 | int main() { 22 | 23 | char a[100]; 24 | 25 | inputString(a, 100, '.'); 26 | cout << "Array: " << a << endl; 27 | 28 | /* 29 | char ch; 30 | // cin >> ch; 31 | ch = cin.get(); 32 | 33 | if (ch == ' ' || ch == '\t' || ch == '\n') { 34 | cout << "You entered whitespace character\n"; 35 | } 36 | else 37 | cout << ch << endl; 38 | */ 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/8_RotateString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/8_RotateString -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/8_RotateString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | char a[1000] = "Coding"; 7 | int len = strlen(a); // Inbuilt function for length 8 | int k = 45; 9 | k %= len; 10 | 11 | // 1. Shift all characters k pos ahead 12 | for (int i = len - 1; i >= 0; --i) 13 | { 14 | a[i + k] = a[i]; 15 | } 16 | 17 | cout << a << endl; 18 | 19 | // 2.Bring the last k characters to front 20 | for (int i = len, j = 0 ; j < k ; ++j, ++i) 21 | { 22 | a[j] = a[i]; 23 | } 24 | 25 | cout << a << endl; 26 | 27 | // 3. Add NULL on len pos 28 | a[len] = '\0'; 29 | cout << a << endl; 30 | 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/input.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | Learning Strings. -------------------------------------------------------------------------------- /L7-Arrays-2DArrays/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L7-Arrays-2DArrays/output.txt -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/1_RecursionBasics: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/1_RecursionBasics -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/1_RecursionBasics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // int fact(int n) { 5 | // // base case 6 | // if (n == 0) return 1; 7 | 8 | // // recursive case 9 | // // fact(n) = n * fact(n-1) 10 | // int chotiProblem = fact(n - 1); // Assume it 11 | // int badiProblem = n * chotiProblem; 12 | 13 | // return badiProblem; 14 | // } 15 | 16 | int fact(int n) { 17 | // base case 18 | if (n == 0) return 1; 19 | 20 | // recursive case 21 | return n * fact(n - 1); 22 | } 23 | 24 | 25 | int main() { 26 | 27 | #ifndef ONLINE_JUDGE 28 | freopen("input.txt", "r", stdin); 29 | freopen("output.txt", "w", stdout); 30 | #endif 31 | 32 | cout << fact(4) << endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/1_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 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/2_ConcatenateStrings: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/2_ConcatenateStrings -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/2_ConcatenateStrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void concatenate(char a[], char b[]) { 6 | int i = strlen(a); 7 | int j = 0; 8 | 9 | int len_b = strlen(b); 10 | 11 | while (j <= len_b) { 12 | a[i++] = b[j++]; 13 | } 14 | } 15 | 16 | int main() { 17 | 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | #endif 22 | 23 | char a[100] = "Coding"; 24 | char b[] = "Blocks"; 25 | 26 | 27 | // concatenate(a, b); // User defined 28 | strcat(a, b); // Inbuilt 29 | cout << a << endl; 30 | 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/2_SumOfN: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/2_SumOfN -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/2_SumOfN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sum(int n) { 5 | if (n == 0) return 0; 6 | 7 | return n + sum(n - 1); 8 | } 9 | 10 | int main() { 11 | #ifndef ONLINE_JUDGE 12 | freopen("input.txt", "r", stdin); 13 | freopen("output.txt", "w", stdout); 14 | #endif 15 | 16 | cout << sum(5); 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/3_Fibo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/3_Fibo -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/3_Fibo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibo(int n) { 5 | if (n <= 1) return n; 6 | 7 | return fibo(n - 1) + fibo(n - 2); 8 | } 9 | 10 | int main() { 11 | 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | cout << fibo(5); 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/3_LargestString: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/3_LargestString -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/3_LargestString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | #ifndef ONLINE_JUDGE 6 | freopen("input.txt", "r", stdin); 7 | freopen("output.txt", "w", stdout); 8 | #endif 9 | 10 | int n, max_len = 0; 11 | char largest[100] = ""; 12 | cin >> n; 13 | 14 | cin.get(); // Number ke baad wale character ko read krlo 15 | // ' ' or '\n' hoga and isse kahi store mat kro, therefore ignore 16 | // ho jaega apne aap 17 | char a[100]; 18 | 19 | for (int i = 0; i < n; ++i) 20 | { 21 | cin.getline(a, 100); 22 | int len_a = strlen(a); 23 | if (len_a > max_len) { 24 | max_len = len_a; 25 | strcpy(largest, a); 26 | } 27 | 28 | // cout << "You entered " << i << " : " << a << endl; 29 | } 30 | 31 | cout << "Largest: " << largest << endl; 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/4_NextPermutation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/4_NextPermutation -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/4_NextPermutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // we are looking to find index a[i] < a[i+1] 5 | int findIndex(char a[]) { 6 | int k = -1; 7 | int lena = strlen(a); 8 | for (int i = lena - 2; i >= 0; --i) 9 | { 10 | if (a[i] < a[i + 1]) { 11 | k = i; 12 | break; 13 | } 14 | } 15 | 16 | return k; 17 | } 18 | 19 | void reverseString(char a[], int i, int j) { 20 | while (i < j) { 21 | swap(a[i], a[j]); 22 | i++; 23 | j--; 24 | } 25 | } 26 | 27 | int main() { 28 | #ifndef ONLINE_JUDGE 29 | freopen("input.txt", "r", stdin); 30 | freopen("output.txt", "w", stdout); 31 | #endif 32 | char a[1000]; 33 | cin >> a; 34 | 35 | int k = findIndex(a); 36 | 37 | if (k == -1) { // string poori max possible string h 38 | // String ko reverse krke print kardo 39 | // input could be: dcba 40 | // reverse karo-> abcd(this is answer) 41 | reverseString(a, 0, strlen(a) - 1); 42 | cout << a << endl; 43 | } 44 | else { 45 | int j = strlen(a) - 1; 46 | while (j > k) { 47 | if (a[j] > a[k]) { 48 | swap(a[j], a[k]); 49 | break; 50 | } 51 | 52 | j--; 53 | } 54 | 55 | reverseString(a, k + 1, strlen(a) - 1); 56 | cout << a << endl; 57 | } 58 | 59 | 60 | return 0; 61 | } 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/4_SumOfArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/4_SumOfArray -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/4_SumOfArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumOfArray(int *a, int n) { 5 | if (n == 0) return 0; 6 | 7 | return a[0] + sumOfArray(a + 1, n - 1); 8 | } 9 | 10 | int main() { 11 | #ifndef ONLINE_JUDGE 12 | freopen("input.txt", "r", stdin); 13 | freopen("output.txt", "w", stdout); 14 | #endif 15 | int a[] = {1, 2, 3, 4, 5}; 16 | int n = sizeof(a) / sizeof(int); 17 | 18 | cout << sumOfArray(a, n) << endl; 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/5_StringToInt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/5_StringToInt -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/5_StringToInt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int stringToInt(char *a, int n) { 5 | // base case 6 | if (n == 0) return 0; 7 | // recursive case 8 | return stringToInt(a, n - 1) * 10 + (a[n - 1] - '0'); 9 | } 10 | 11 | int main() { 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | char a[] = "1234"; 18 | int x = stringToInt(a, strlen(a)); 19 | 20 | cout << x + 1 << endl; // 1235 21 | return 0; 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/6_CharToInt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L8-CharacterArrays-Recursion/6_CharToInt -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/6_CharToInt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | #ifndef ONLINE_JUDGE 6 | freopen("input.txt", "r", stdin); 7 | freopen("output.txt", "w", stdout); 8 | #endif 9 | int x = '2' - '0'; 10 | cout << x << endl; 11 | 12 | return 0; 13 | } 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/input.txt: -------------------------------------------------------------------------------- 1 | cfedba 2 | -------------------------------------------------------------------------------- /L8-CharacterArrays-Recursion/output.txt: -------------------------------------------------------------------------------- 1 | 1235 2 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/1_SortArray: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L9-Recursion_ClassTest/1_SortArray -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/1_SortArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int ones = 0, twos = 0, zeros = 0; 5 | int n; 6 | cin >> n; 7 | for (int i = 0 ; i < n ; i++) { 8 | int x ; cin >> x; 9 | 10 | if (x == 0) zeros ++; 11 | else if (x == 1) ones++; 12 | else twos++; 13 | } 14 | 15 | while (zeros) { 16 | cout << 0 << " "; 17 | zeros--; 18 | } 19 | 20 | while (ones) { 21 | cout << 1 << " "; 22 | ones--; 23 | } 24 | 25 | while (twos) { 26 | cout << 2 << " "; 27 | twos--; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/2_IntersectionOfArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int a[100005]; 4 | int b[100005]; 5 | int ispresent[100005] = {0}; 6 | 7 | int main() { 8 | 9 | 10 | int n, m; cin >> n >> m; 11 | 12 | for (int i = 0; i < n; ++i) 13 | { 14 | int x; cin >> x; 15 | ispresent[x]++; 16 | } 17 | 18 | for (int i = 0; i < m; ++i) 19 | { 20 | int x; cin >> x; 21 | if (ispresent[x] > 0) { 22 | cout << x << " "; 23 | ispresent[x] --; 24 | } 25 | } 26 | 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/3_HoodiesAtCB: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L9-Recursion_ClassTest/3_HoodiesAtCB -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/3_HoodiesAtCB.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | queue q[5]; 9 | int entryPoint[5] = {0}; 10 | int cnt = 1; 11 | int n; 12 | cin >> n; 13 | for (int i = 0; i < n; ++i) 14 | { 15 | char ch; cin >> ch; 16 | if (ch == 'E') { 17 | int courseNumber, rollNumber; 18 | cin >> courseNumber >> rollNumber; 19 | if (entryPoint[courseNumber] == 0) { 20 | // Yeh course wala banda pehle nhi aya koi 21 | q[courseNumber].push(rollNumber); 22 | entryPoint[courseNumber] = cnt++; 23 | } 24 | else { 25 | // Yeh course wale bande ka dost line mei hai 26 | q[courseNumber].push(rollNumber); 27 | } 28 | } 29 | else { 30 | // Iska matlab Tshirt deni hai banda bahar nikalo 31 | int indx; 32 | int ans = INT_MAX; 33 | for (int i = 1; i <= 4; ++i) 34 | { 35 | if (entryPoint[i] > 0 and entryPoint[i] < ans) { 36 | ans = entryPoint[i]; 37 | indx = i; 38 | } 39 | } 40 | 41 | int courseNumber = indx; 42 | cout << courseNumber << " " << q[courseNumber].front() << endl; 43 | q[courseNumber].pop(); 44 | if (q[courseNumber].size() == 0) { 45 | entryPoint[courseNumber] = 0; 46 | } 47 | } 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/4_PlayingCards: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L9-Recursion_ClassTest/4_PlayingCards -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/4_PlayingCards.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | bool a[10000005]; 6 | int arr[1000005]; 7 | vector primes; 8 | 9 | void sieve() { 10 | for (int i = 0; i < 10000005; ++i)a[i] = true; 11 | 12 | for (int i = 2; i * i < 10000005; ++i) 13 | { 14 | if (a[i] == true) { 15 | for (int j = i * i; j < 10000005; j += i) 16 | { 17 | a[j] = false; 18 | } 19 | primes.push_back(i); 20 | } 21 | 22 | } 23 | } 24 | 25 | int main() { 26 | 27 | sieve(); 28 | 29 | int n, queries; 30 | cin >> n; 31 | cin >> queries; 32 | stack S, A, B; 33 | 34 | for (int i = 0; i < n; ++i) 35 | { 36 | cin >> arr[i]; 37 | S.push(arr[i]); 38 | } 39 | 40 | for (int i = 0; i < queries; ++i) 41 | { 42 | int currentPrime = primes[i]; 43 | 44 | while (!S.empty()) { 45 | int e = S.top(); 46 | if (e % currentPrime == 0) B.push(e); 47 | else A.push(e); 48 | 49 | S.pop(); 50 | } 51 | 52 | while (!B.empty()) { 53 | cout << B.top() << endl; 54 | B.pop(); 55 | } 56 | // Taaki agli query mei phr se agle prime number se divide krke 57 | // B mei daaloge else A mei 58 | S = A; 59 | while (!A.empty()) { 60 | A.pop(); 61 | } 62 | } 63 | 64 | while (!S.empty()) { 65 | cout << S.top() << endl; 66 | S.pop(); 67 | } 68 | 69 | 70 | 71 | return 0; 72 | } 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/5_First7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L9-Recursion_ClassTest/5_First7 -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/5_First7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int first7(int *a, int n) { 5 | // base case 6 | if (n == 0) return -1; 7 | 8 | // recursive case 9 | if (a[0] == 7) return 0; 10 | int indx = first7(a + 1, n - 1); 11 | 12 | if (indx == -1) return -1; 13 | 14 | return indx + 1; 15 | } 16 | 17 | int main() { 18 | 19 | int a[] = {1, 2, 7, 3, 4, 5, 7, 7}; 20 | int n = sizeof(a) / sizeof(int); 21 | 22 | 23 | cout << first7(a, n) << endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/6_Last7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L9-Recursion_ClassTest/6_Last7 -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/6_Last7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // n is number of elements 5 | int last7(int *a, int n) { 6 | // base case 7 | if (n == 0) return -1; 8 | 9 | // recursive case 10 | if (a[n - 1] == 7) return n - 1; 11 | 12 | return last7(a, n - 1); 13 | } 14 | 15 | int main() { 16 | 17 | int a[] = {4, 5, 7, 7, 2, 3}; 18 | int n = sizeof(a) / sizeof(int); 19 | 20 | cout << last7(a, n) << endl; 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/7_All7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/L9-Recursion_ClassTest/7_All7 -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/7_All7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printAll7(int *a, int i, int n) { 5 | // base case 6 | if (i == n) { 7 | return; 8 | } 9 | 10 | // recursive case 11 | // Ek index par meine check krke index ko print kar diya 12 | if (a[i] == 7) cout << i << " "; 13 | 14 | // Baaki ke indexes print karne ko recursion ko bol diya 15 | printAll7(a, i + 1, n); 16 | } 17 | 18 | int main() { 19 | 20 | int a[] = {2, 3, 7, 6, 5, 7, 9, 7}; 21 | int n = sizeof(a) / sizeof(int); 22 | 23 | printAll7(a, 0, n); 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /L9-Recursion_ClassTest/SortArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n; 7 | cin >> n; 8 | int ones = 0, zeros = 0, twos = 0; 9 | 10 | for (int i = 0; i < n; ++i) 11 | { 12 | int x; cin >> x; 13 | if (x == 0) zeros++; 14 | else if (x == 1) ones++; 15 | else twos++; 16 | } 17 | 18 | for (int i = 0; i < zeros; ++i) 19 | { 20 | cout << 0 << " "; 21 | } 22 | 23 | for (int i = 0; i < ones; ++i) 24 | { 25 | cout << 1 << " "; 26 | } 27 | 28 | for (int i = 0; i < twos; ++i) 29 | { 30 | cout << 2 << " "; 31 | } 32 | 33 | 34 | 35 | return 0; 36 | } 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Lec-1-Arrays: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lec-1-Arrays -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/10_OkabeAndBoxes: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/10_OkabeAndBoxes -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/10_OkabeAndBoxes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | string str; 8 | stack s; 9 | 10 | int val = 1; 11 | int reorder = 0; 12 | 13 | int n; cin >> n; 14 | 15 | for (int i = 0; i < 2 * n; ++i) 16 | { 17 | cin >> str; 18 | if (str == "add") { 19 | int number; cin >> number; 20 | s.push(number); 21 | } 22 | else { 23 | if (!s.empty()) { 24 | if (val == s.top()) { 25 | s.pop(); 26 | } 27 | else { 28 | reorder++; 29 | s = {}; // make stack empty 30 | } 31 | } 32 | 33 | val++; 34 | } 35 | } 36 | 37 | cout << reorder << endl; 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/11_Queue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/11_Queue -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/11_Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class node { 5 | public: 6 | int data; 7 | node* next; 8 | node(int d) { 9 | data = d; 10 | next = NULL; 11 | } 12 | }; 13 | 14 | class Queue { 15 | public: 16 | node* head, *tail; 17 | Queue() { 18 | head = tail = NULL; 19 | } 20 | 21 | void push(int d) { 22 | if (!head) { 23 | head = tail = new node(d); 24 | } 25 | else { 26 | node *n = new node(d); 27 | tail->next = n; 28 | tail = n; 29 | } 30 | } 31 | 32 | void pop() { 33 | if (!head) return; 34 | else if (!head->next) { 35 | delete head; 36 | head = tail = NULL; 37 | } 38 | else { 39 | node* temp = head; 40 | head = head->next; 41 | delete temp; 42 | } 43 | } 44 | 45 | int front() { 46 | return head->data; 47 | } 48 | 49 | bool empty() { 50 | return head == NULL; 51 | } 52 | 53 | }; 54 | 55 | int main() { 56 | 57 | // Queue q; 58 | queue q; 59 | 60 | q.push(1); 61 | q.push(2); 62 | q.push(3); 63 | q.push(4); 64 | q.push(5); 65 | 66 | while (!q.empty()) { 67 | cout << q.front() << " "; 68 | q.pop(); 69 | } 70 | 71 | 72 | return 0; 73 | } 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/12_CircularQueue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/12_CircularQueue -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/12_CircularQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue { 5 | public: 6 | int *a; 7 | int n; 8 | int cs; 9 | int f , r; 10 | Queue(int N) { 11 | n = N; 12 | a = new int[n]; 13 | f = 0; 14 | r = n - 1; 15 | cs = 0; 16 | } 17 | 18 | void push(int d) { 19 | if (cs < n) { 20 | r = (r + 1) % n; 21 | a[r] = d; 22 | cs++; 23 | } 24 | else cout << "Overflow\n"; 25 | } 26 | 27 | void pop() { 28 | if (cs > 0) { 29 | f = (f + 1) % n; 30 | cs--; 31 | } 32 | else cout << "Undeflow\n"; 33 | } 34 | 35 | bool empty() { 36 | return cs == 0; 37 | } 38 | 39 | int front() { 40 | return a[f]; 41 | } 42 | }; 43 | 44 | int main() { 45 | Queue q(4); 46 | q.push(1); 47 | q.push(2); 48 | q.push(3); 49 | q.push(4); 50 | q.push(5); 51 | 52 | while (!q.empty()) { 53 | cout << q.front() << " "; 54 | q.pop(); 55 | } 56 | 57 | q.pop(); 58 | 59 | 60 | 61 | return 0; 62 | } 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/132Pattern.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool find132pattern(vector& nums) { 4 | stack st; 5 | int C = INT_MIN; // C se chota dhoondhna padega, therefore make it MINIMUM 6 | int n = nums.size(); 7 | for (int i = n - 1; i >= 0; i--) { 8 | if (nums[i] < C) { 9 | return true; 10 | } 11 | 12 | // Bump aane par C ko update kardo 13 | while (!st.empty() && st.top() < nums[i] ) { 14 | C = st.top(); 15 | st.pop(); 16 | } 17 | 18 | st.push(nums[i]); 19 | } 20 | 21 | return false; 22 | } 23 | }; -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/1_Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/1_Test -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/1_Test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | stack s; 9 | 10 | int a[] = {6, 2, 1, 3, 4, 4, 7}; 11 | int n = sizeof(a) / sizeof(int); 12 | 13 | int nextgreater[100]; 14 | 15 | for (int i = 0; i < n; ++i) 16 | { 17 | while (!s.empty() and a[i] > a[s.top()]) { 18 | nextgreater[s.top()] = i; 19 | s.pop(); 20 | } 21 | 22 | s.push(i); 23 | } 24 | 25 | while (!s.empty()) { 26 | nextgreater[s.top()] = n; 27 | s.pop(); 28 | } 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | cout << nextgreater[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-15-StackAndQueues/2_NextGreater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | stack s; 9 | 10 | int a[] = {6, 2, 1, 3, 4, 4, 7}; 11 | int n = sizeof(a) / sizeof(int); 12 | 13 | int nextgreater[100]; 14 | 15 | for (int i = 0; i < n; ++i) 16 | { 17 | while (!s.empty() and a[i] > a[s.top()]) { 18 | nextgreater[s.top()] = i; 19 | s.pop(); 20 | } 21 | 22 | s.push(i); 23 | } 24 | 25 | while (!s.empty()) { 26 | nextgreater[s.top()] = n; 27 | s.pop(); 28 | } 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | cout << nextgreater[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-15-StackAndQueues/3_PreviousGreater: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/3_PreviousGreater -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/3_PreviousGreater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | stack s; 8 | 9 | int a[] = {6, 2, 1, 3, 4, 4, 7}; 10 | int n = sizeof(a) / sizeof(int); 11 | 12 | int previousGreater[100]; 13 | 14 | for (int i = n - 1; i >= 0; --i) 15 | { 16 | while (!s.empty() and a[i] > a[s.top()]) { 17 | previousGreater[s.top()] = i; 18 | s.pop(); 19 | } 20 | 21 | s.push(i); 22 | } 23 | 24 | while (!s.empty()) { 25 | previousGreater[s.top()] = -1; 26 | s.pop(); 27 | } 28 | 29 | for (int i = 0; i < n; ++i) 30 | { 31 | cout << previousGreater[i] << " "; 32 | } 33 | cout << endl; 34 | 35 | 36 | return 0; 37 | } 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/4_LargestRectangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestRectangleArea(vector& a) { 4 | int n = a.size(); 5 | stack s; 6 | // PREVIOUS SMALLER 7 | vector previousSmaller(n + 1); 8 | for (int i = n - 1; i >= 0; --i) 9 | { 10 | while (!s.empty() and a[i] < a[s.top()]) { 11 | previousSmaller[s.top()] = i; 12 | s.pop(); 13 | } 14 | 15 | s.push(i); 16 | } 17 | 18 | while (!s.empty()) { 19 | previousSmaller[s.top()] = -1; 20 | s.pop(); 21 | } 22 | 23 | // NEXT SMALLER 24 | vector nextSmaller(n + 1); 25 | for (int i = 0; i < n; ++i) 26 | { 27 | while (!s.empty() and a[i] < a[s.top()]) { 28 | nextSmaller[s.top()] = i; 29 | s.pop(); 30 | } 31 | 32 | s.push(i); 33 | } 34 | 35 | while (!s.empty()) { 36 | nextSmaller[s.top()] = n; 37 | s.pop(); 38 | } 39 | 40 | // MAX AREA 41 | int area = 0; 42 | for (int i = 0; i < n; ++i) 43 | { 44 | area = max(area, (nextSmaller[i] - previousSmaller[i] - 1) * a[i]); 45 | } 46 | 47 | return area; 48 | } 49 | }; -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/5_NextSmaller: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/5_NextSmaller -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/5_NextSmaller.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | 8 | stack s; 9 | 10 | int a[] = {6, 2, 1, 3, 4, 4, 7}; 11 | int n = sizeof(a) / sizeof(int); 12 | 13 | int nextSmaller[100]; 14 | 15 | for (int i = 0; i < n; ++i) 16 | { 17 | while (!s.empty() and a[i] < a[s.top()]) { 18 | nextSmaller[s.top()] = i; 19 | s.pop(); 20 | } 21 | 22 | s.push(i); 23 | } 24 | 25 | while (!s.empty()) { 26 | nextSmaller[s.top()] = n; 27 | s.pop(); 28 | } 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | cout << nextSmaller[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-15-StackAndQueues/6_PreviousSmaller: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/6_PreviousSmaller -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/6_PreviousSmaller.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | stack s; 8 | 9 | int a[] = {6, 2, 1, 3, 4, 4, 7}; 10 | int n = sizeof(a) / sizeof(int); 11 | 12 | int previousSmaller[100]; 13 | 14 | for (int i = n - 1; i >= 0; --i) 15 | { 16 | while (!s.empty() and a[i] < a[s.top()]) { 17 | previousSmaller[s.top()] = i; 18 | s.pop(); 19 | } 20 | 21 | s.push(i); 22 | } 23 | 24 | while (!s.empty()) { 25 | previousSmaller[s.top()] = -1; 26 | s.pop(); 27 | } 28 | 29 | for (int i = 0; i < n; ++i) 30 | { 31 | cout << previousSmaller[i] << " "; 32 | } 33 | cout << endl; 34 | 35 | 36 | return 0; 37 | } 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/7_OnlineStockSpan.cpp: -------------------------------------------------------------------------------- 1 | class Pair { 2 | public: 3 | int price; 4 | int span; 5 | }; 6 | 7 | class StockSpanner { 8 | public: 9 | stack s; 10 | 11 | StockSpanner() { 12 | 13 | } 14 | 15 | int next(int price) { 16 | int x = 1; 17 | while (!s.empty() and price >= s.top().price) { 18 | x += s.top().span; 19 | s.pop(); 20 | } 21 | 22 | s.push({price, x}); 23 | return x; 24 | } 25 | }; 26 | 27 | /** 28 | * Your StockSpanner object will be instantiated and called as such: 29 | * StockSpanner* obj = new StockSpanner(); 30 | * int param_1 = obj->next(price); 31 | */ -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/9_BalancedParanthesis: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kartik-Mathur/bpit-batch-2025Jan/e483f3d6d24645803e2f5666033224c53b909ff5/Lecture-15-StackAndQueues/9_BalancedParanthesis -------------------------------------------------------------------------------- /Lecture-15-StackAndQueues/9_BalancedParanthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isBalanced(string &str) { 6 | stack s; 7 | 8 | for (int i = 0 ; i < str.size(); i++) { 9 | char ch = str[i]; 10 | switch (ch) { 11 | case '(': 12 | case '{': 13 | case '[': 14 | s.push(ch); 15 | break; 16 | case ')': 17 | if (!s.empty() and s.top() == '(') { 18 | s.pop(); 19 | } 20 | else return false; 21 | break; 22 | case '}': 23 | if (!s.empty() and s.top() == '{') { 24 | s.pop(); 25 | } 26 | else return false; 27 | break; 28 | case ']': 29 | if (!s.empty() and s.top() == '[') { 30 | s.pop(); 31 | } 32 | else return false; 33 | break; 34 | } 35 | } 36 | 37 | if (s.empty()) return true; 38 | 39 | return false; 40 | } 41 | 42 | int main() { 43 | 44 | string s = "[a+b*(c+d)+d*{e+f}]"; 45 | if (isBalanced(s)) cout << "Balanced\n"; 46 | else cout << "Not Balanced\n"; 47 | 48 | 49 | return 0; 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bpit-batch-2025Jan 2 | --------------------------------------------------------------------------------