├── .gitignore ├── .vscode ├── launch.json ├── settings.json └── tasks.json ├── 450dsa ├── AddTwoNumbersGivenAsLinkedList.cpp ├── CheckLinkedIsCircularOrNot.cpp ├── CheckPalindrome.cpp ├── MergeSortLinkedList.cpp ├── MiddleOfLinkedList.cpp ├── RemoveNFromBackOfLinkedList.cpp ├── ReverseLinkedListInKGroups.cpp ├── RotateALinkedList.cpp ├── WriteAProgramToReverseLinkedList.cpp ├── file.txt ├── floydCycleDetection.cpp └── tempCodeRunnerFile.cpp ├── C100BitGivenNumIsPowerOf2.cpp ├── C100BitManipulationBasic.cpp ├── C101BitCountOnesInBinaryRepresentationOfANum.cpp ├── C102BitGenrateAllPossibleSubsetOfAnSet.cpp ├── C103ImplementStack.cpp ├── C104ReverseASentenceUsingStack.cpp ├── C105ReverseAStack.cpp ├── C106InfixPrefixPostfix.cpp ├── C107InfixToPostfix.cpp ├── C108InfixToPrefix.cpp ├── C109BalanceParanthesis.cpp ├── C10TernaryOperator.cpp ├── C110OueueUsingArray.cpp ├── C111LinkedListrImplementationOFQueue.cpp ├── C112QueueUsing2Stack.cpp ├── C113QueueUsingOneStack.cpp ├── C114StackUsing2queue.cpp ├── C115ReverseStackUsingRec.cpp ├── C116StackSorting.cpp ├── C117nextSmallestElement.cpp ├── C11ReferenceOperator.cpp ├── C12PatternPrintRectangle.cpp ├── C13PatternPrintHollowRectangle.cpp ├── C14PatternPrintInvertedHalfPiramid.cpp ├── C15PrintPatternHalfPyramid80Rotation.cpp ├── C16PattenPrintHalfPyramidUsingNumber.cpp ├── C17PatternPrintFloydsTriangle.cpp ├── C18PatternPrintButterfly.cpp ├── C19PatternPrintInvertedPattern.cpp ├── C1Demo1.cpp ├── C20PatternPrint0_1Pattern.cpp ├── C21PatternPrintRhombusPattern.cpp ├── C22PatternPrintNumberPattern.cpp ├── C23PatternPrintPalindromicPattern.cpp ├── C24PatternPrintStarPattern.cpp ├── C25PatternPrintZigZagPAttern.cpp ├── C26CheckPrime.cpp ├── C27ReverseANumber.cpp ├── C28CheckArmstrongNumber.cpp ├── C29function.cpp ├── C2DataType.cpp ├── C30findPrimeNumInRangeFunction.cpp ├── C31FibenacciSequenceUsing.cpp ├── C33FactorialUsingFunction.cpp ├── C34BinaryCofficientCalculateNCR.cpp ├── C35PatternPascalTriangle.cpp ├── C36SumOfNNaturanNumber.cpp ├── C37PythagoreanTriplet.cpp ├── C38BinaryToDecimal.cpp ├── C39OctalToDecimal.cpp ├── C3InputAndOutput.cpp ├── C40HexadecimalToDecimal.cpp ├── C41DecimalToBinary.cpp ├── C42DecimalToOctal.cpp ├── C43DecimalToHexadecimal.cpp ├── C44Check.cpp ├── C45AddwoBinaryNumber.cpp ├── C46Array.cpp ├── C47MaxMinElementInArray.cpp ├── C48SearchingInArray.cpp ├── C49BinarySearchInArray.cpp ├── C4IfAndElse.cpp ├── C50SelectionSort.cpp ├── C51BubbleSort.cpp ├── C52InsertionSort.cpp ├── C53ArrayChallengeMaxTilLI.cpp ├── C54SumOfAllSubArrays.cpp ├── C55ProblemLongesqtArithmeticSubArray.cpp ├── C56ProblemRecordBreaker.cpp ├── C57ProblemFirstRepeatingElement.cpp ├── C58ProblemSubArrayWithGivenSum.cpp ├── C59ProblemSmallestPositiveMissingNumber.cpp ├── C5MaxNumCheck.cpp ├── C60PrintAllSubarray.cpp ├── C61PrintAllSubArray2.cpp ├── C63ProblemSubArrayWhichHasMaximumSum.cpp ├── C64ProblemMaxSubArray.cpp ├── C65ProblemMaxSubArraySumByCummulativeSum.cpp ├── C66ProblemMaxSubArrayByKadensAlgorithm.cpp ├── C67ProblemHackerRankDay20.cpp ├── C68ProblemMaxSubArrayAgainKadanesAlgoritm.cpp ├── C69ProblemMaximumCircularSubArraySum.cpp ├── C6Loops.cpp ├── C70ProblemMaximumCircularSubArraySumOptimizeApproach.cpp ├── C71ProblemPairSumProblemBryteForce.cpp ├── C72ProblmePairSumOptimalApprochForOnlyPositiveArray.cpp ├── C73ProblmePairSumOptimalApprochForSortedArray.cpp ├── C74PairSumProblem.cpp ├── C75DimensionalArray.cpp ├── C76SearchingIn2DArray.cpp ├── C77SpatialOrderPrint.cpp ├── C78MatrixMultiplication.cpp ├── C79MatrixSearch.cpp ├── C7JumpsInLoops.cpp ├── C80CharArray.cpp ├── C81CharArrayInputOutput.cpp ├── C82CheckPalindromeBYCharArray.cpp ├── C83CheckLArgetWordInArray.cpp ├── C84Pointer.cpp ├── C85ArrayAndPointers.cpp ├── C86PointerToPointer.cpp ├── C87PointerInFunction.cpp ├── C88Heap.cpp ├── C89HeapArray.cpp ├── C8PrintPrimeInRange.cpp ├── C90String.cpp ├── C91InputStringWithSpaces.cpp ├── C92StringMethods.cpp ├── C93Compare2String.cpp ├── C94EraseCharFromString.cpp ├── C95FindAndInsertAndEraseEleOnString.cpp ├── C96SubstroingFromString.cpp ├── C97Str2IntAndInt2Str.cpp ├── C98SortAnString.cpp ├── C99Chngz2UpperAndLowerCase.cpp ├── C9SwitchStatements.cpp ├── CppStl ├── C10AlgorithmStl.cpp ├── C1ArrayStl.cpp ├── C2vectorStl.cpp ├── C3Deque.cpp ├── C4ListStl.cpp ├── C5StackStl.cpp ├── C6QueueStl.cpp ├── C7PriorityQueueStl.cpp ├── C8SetStl.cpp └── C9MapStl.cpp ├── HelpMla.cpp ├── LinkedList ├── L0Node.cpp ├── L1SingleLinkedList.cpp └── L2DoublyLinkedList.cpp ├── OOPS ├── O10SingleInheritance.cpp ├── O11MultilevelInheritance.cpp ├── O12MultipleInheritance.cpp ├── O13HeierchicalInheritance.cpp ├── O14HybridInheritance.cpp ├── O15MultipleInheritanceAmbiguity.cpp ├── O16CompiletimePolymorphismEx.cpp ├── O16OperatorOverloading.md ├── O17RuntimePolymorphismEx.cpp ├── O1BlankClass.cpp ├── O20AbstractionConcept.cpp ├── O2AccesDataMembersInClass.cpp ├── O3BasicClassOperation.cpp ├── O4StaticDynamicAllocation.cpp ├── O5ConstructorInCpp.cpp ├── O6ConstructorDeconstructor.cpp ├── O7StaticMethodAndFunction.cpp ├── O8EncapsulationEx.cpp ├── O9InheritanceEx.cpp └── tempCodeRunnerFile.cpp ├── Recursion ├── .cph │ └── .R23RatInMazeUsingRecursion.cpp_9d58cd34a9f076988e7f2776c58c2452.prob ├── R10BinarySearchUsingRecursion.cpp ├── R11FirstAndLastOccurence.cpp ├── R12FindTotalOccurence.cpp ├── R13PeakInMountainArrayUsingRecursion.cpp ├── R14R5CheckPalindromeUsingRecursion.cpp ├── R15R5SolvePowerOptimizeUsingRecursion.cpp ├── R16BubbleSortUsingRecursion.cpp ├── R16ReverseStringUsingRecursion.cpp ├── R17MergeSortUsingRecursion.cpp ├── R18QuickSort.cpp ├── R19POwerSetUsingRecursion.cpp ├── R1findpower2.cpp ├── R20SubSequencesUsingRecursion.cpp ├── R21PhoneKeyPadProblemUsingRecursion.cpp ├── R22PermutationOfAnStringUsingRecursion.cpp ├── R23RatInMazeUsingRecursion.cpp ├── R2Factorial.cpp ├── R3PrintCounting.cpp ├── R4FibonaciOfNteram.cpp ├── R5OnLadderSteps.cpp ├── R6SayDigit.cpp ├── R7CheckArrayIsSortedRecursion.cpp ├── R8ArraySumUsingRecursion.cpp ├── R9LinearSearchUsingRecursion.cpp ├── temp.cpp └── tempCodeRunnerFile.cpp └── codehelp ├── C1BinarySearch.cpp ├── C2FirstAndLastOccurenceOfElementInSortedArray.cpp ├── C3NoOfOccurenceOfKeyInSortedArray.cpp ├── C4FindPivotElementInSortedRotatedArray.cpp ├── C4PeakIndexInMountainArray.cpp └── temp.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !/**/ 3 | !*.* 4 | tempCodeRunnerFile* 5 | !/codehelp/**/ 6 | !/codehelp/*.cpp 7 | /codehelp/tempCodeRunnerFile* 8 | .cph/ 9 | 10 | !/*/*.cpp -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "g++ - Build and debug active file", 9 | "type": "cppdbg", 10 | "request": "launch", 11 | "program": "${fileDirname}/${fileBasenameNoExtension}", 12 | "args": [], 13 | "stopAtEntry": false, 14 | "cwd": "${fileDirname}", 15 | "environment": [], 16 | "externalConsole": false, 17 | "MIMode": "gdb", 18 | "setupCommands": [ 19 | { 20 | "description": "Enable pretty-printing for gdb", 21 | "text": "-enable-pretty-printing", 22 | "ignoreFailures": true 23 | }, 24 | { 25 | "description": "Set Disassembly Flavor to Intel", 26 | "text": "-gdb-set disassembly-flavor intel", 27 | "ignoreFailures": true 28 | } 29 | ], 30 | "preLaunchTask": "C/C++: g++ build active file ver(1)", 31 | "miDebuggerPath": "/usr/bin/gdb" 32 | } 33 | ] 34 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "ostream": "cpp", 5 | "chrono": "cpp", 6 | "random": "cpp", 7 | "limits": "cpp", 8 | "valarray": "cpp", 9 | "span": "cpp", 10 | "variant": "cpp", 11 | "vector": "cpp" 12 | }, 13 | "C_Cpp.errorSquiggles": "Disabled" 14 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++ build active file", 6 | "command": "/usr/bin/g++", 7 | "args": [ 8 | "-g", 9 | "${file}", 10 | "-o", 11 | "${fileDirname}/${fileBasenameNoExtension}" 12 | ], 13 | "options": { 14 | "cwd": "${fileDirname}" 15 | }, 16 | "problemMatcher": [ 17 | "$gcc" 18 | ], 19 | "group": "build", 20 | "detail": "Task generated by Debugger." 21 | }, 22 | { 23 | "type": "cppbuild", 24 | "label": "C/C++: g++ build active file ver(1)", 25 | "command": "/usr/bin/g++", 26 | "args": [ 27 | "-fdiagnostics-color=always", 28 | "-g", 29 | "${file}", 30 | "-o", 31 | "${fileDirname}/${fileBasenameNoExtension}" 32 | ], 33 | "options": { 34 | "cwd": "${fileDirname}" 35 | }, 36 | "problemMatcher": [ 37 | "$gcc" 38 | ], 39 | "group": { 40 | "kind": "build", 41 | "isDefault": true 42 | }, 43 | "detail": "Task generated by Debugger." 44 | } 45 | ], 46 | "version": "2.0.0" 47 | } -------------------------------------------------------------------------------- /450dsa/AddTwoNumbersGivenAsLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | Node* AddTwoNumGivenAsLinkedList(Node *&head1 , Node*&head2){ 109 | Node *temp1 = head1; 110 | Node *temp2 = head2; 111 | 112 | Node *start = new Node(0); 113 | Node *SumLinkedList = start; 114 | Node *tail = start; 115 | 116 | int sum = 0; 117 | int carry = 0; 118 | while(temp1 != NULL || temp2 != NULL || carry != 0){ 119 | if(temp1 != NULL){ 120 | sum += temp1->data; 121 | temp1 = temp1->next; 122 | } 123 | if (temp2 != NULL){ 124 | sum += temp2->data; 125 | temp2 = temp2->next; 126 | } 127 | if(carry != 0){ 128 | sum += carry; 129 | } 130 | carry = sum/10; 131 | InsertAtTail(tail , sum%10); 132 | sum = 0; 133 | } 134 | 135 | return SumLinkedList->next; 136 | } 137 | 138 | 139 | 140 | int main() 141 | { 142 | 143 | Node *start1 = new Node(2); 144 | Node *head1 = start1; 145 | Node *tail1 = start1; 146 | InsertAtHead(head1, 4); 147 | InsertAtHead(head1, 3); 148 | InsertAtTail(tail1, 0); 149 | Print(head1); 150 | 151 | Node *start2 = new Node(3); 152 | Node *head2 = start2; 153 | Node *tail2 = start2; 154 | InsertAtHead(head2, 7); 155 | InsertAtHead(head2, 9); 156 | InsertAtHead(head2, 2); 157 | InsertAtHead(head2, 0); 158 | Print(head2); 159 | 160 | Node *sumOf2 = AddTwoNumGivenAsLinkedList(head1 , head2); 161 | Print(sumOf2); 162 | 163 | 164 | return 0; 165 | } -------------------------------------------------------------------------------- /450dsa/CheckLinkedIsCircularOrNot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | bool isCircular(Node *head){ 109 | if (head == NULL){ 110 | return true; 111 | } 112 | 113 | Node *temp = head->next; 114 | 115 | while(temp != NULL && temp != head){ 116 | temp = temp->next; 117 | } 118 | if(temp == head){ 119 | return true; 120 | } 121 | return false; 122 | } 123 | 124 | int main() 125 | { 126 | 127 | // create Nod 128 | Node *start = new Node(10); 129 | 130 | Node *head = start; 131 | Node *tail = start; 132 | 133 | // Insert At Head 134 | InsertAtHead(head, 29); 135 | InsertAtHead(head, 3); 136 | InsertAtHead(head, 1); 137 | InsertAtHead(head, 9); 138 | InsertAtTail(tail, 0); 139 | InsertAtTail(tail, 23); 140 | InsertAtPosition(head, tail, 5, 3); 141 | InsertAtPosition(head, tail, 55, 1); 142 | InsertAtPosition(head, tail, 999, 50); 143 | InsertAtTail(tail, 600); 144 | Print(head); 145 | 146 | cout<next = head; 150 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | Node* ReverseLinkedList(Node *&head){ 109 | if (head == NULL && head->next == NULL){ 110 | return head; 111 | } 112 | Node *prev = NULL; 113 | Node *next; 114 | Node *curr = head; 115 | 116 | while(curr != NULL){ 117 | next = curr->next; 118 | curr->next = prev; 119 | 120 | prev = curr; 121 | curr = next; 122 | } 123 | return prev; 124 | 125 | } 126 | 127 | Node * findMid(Node*&head){ 128 | Node *slow = head; 129 | Node *high = head; 130 | while(high != NULL){ 131 | slow = slow->next; 132 | high = high->next; 133 | if (high!=NULL){ 134 | high = high->next; 135 | } 136 | } 137 | return slow; 138 | } 139 | 140 | bool CheckPalindrome(Node* &head){ 141 | Node *mid = findMid(head); 142 | // reverse from mid 143 | Print(mid); 144 | Node *newHead = ReverseLinkedList(mid); 145 | Print(head); 146 | Print(newHead); 147 | 148 | Node *temp1 = head; 149 | Node *temp2 = newHead; 150 | 151 | while(temp1 != NULL || temp2 != NULL ){ 152 | if (temp1->data == newHead->data){ 153 | return true; 154 | } 155 | if(temp1->data != temp2->data && temp1->next->data != mid->data && mid->data != newHead->data ){ 156 | cout<data<data<next; 162 | } 163 | temp1 = temp1->next; 164 | } 165 | return 1; 166 | } 167 | 168 | int main() 169 | { 170 | 171 | Node *start = new Node(1); 172 | Node *head = start; 173 | Node *tail = start; 174 | InsertAtTail(tail, 2); 175 | InsertAtTail(tail, 3); 176 | InsertAtTail(tail, 4); 177 | InsertAtTail(tail, 4); 178 | InsertAtTail(tail, 2); 179 | InsertAtTail(tail, 1); 180 | Print(head); 181 | 182 | cout< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | Node *findMid(Node *head) 109 | { 110 | Node *slow = head; 111 | Node *high = head->next; 112 | while (high != NULL && high->next != NULL) 113 | { 114 | slow = slow->next; 115 | high = high->next->next; 116 | } 117 | return slow; 118 | } 119 | 120 | Node *Merge(Node *left, Node *right) 121 | { 122 | if (left == NULL) 123 | { 124 | return right; 125 | } 126 | if (right == NULL) 127 | { 128 | return left; 129 | } 130 | 131 | Node *ans = new Node(-1); 132 | Node *temp = ans; 133 | while (left != NULL && right != NULL) 134 | { 135 | if (left->data < right->data) 136 | { 137 | temp->next = left; 138 | temp = left; 139 | left = left->next; 140 | } 141 | else 142 | { 143 | temp->next = right; 144 | temp = right; 145 | right = right->next; 146 | } 147 | } 148 | while (left != NULL) 149 | { 150 | temp->next = left; 151 | temp = left; 152 | left = left->next; 153 | } 154 | while (right != NULL) 155 | { 156 | temp->next = right; 157 | temp = right; 158 | right = right->next; 159 | } 160 | ans = ans->next ; 161 | return ans; 162 | } 163 | 164 | Node *MergeSort(Node *head) 165 | { 166 | if (head == NULL || head -> next == NULL) 167 | { 168 | return head; 169 | } 170 | Node *mid = findMid(head); 171 | Node *LeftHalf = head; 172 | Node *RightHalf = mid->next; 173 | mid->next = NULL; 174 | 175 | Node* left = MergeSort(LeftHalf); 176 | Node* right = MergeSort(RightHalf); 177 | 178 | Node *mergeLinkedList = Merge(left , right); 179 | return mergeLinkedList; 180 | } 181 | 182 | int main() 183 | { 184 | 185 | // create Nod 186 | Node *start = new Node(12); 187 | Node *head = start; 188 | Node *tail = start; 189 | InsertAtHead(head, 29); 190 | InsertAtHead(head, 3); 191 | InsertAtHead(head, 1); 192 | InsertAtHead(head, 9); 193 | InsertAtTail(tail, 0); 194 | InsertAtTail(tail, 23); 195 | InsertAtPosition(head, tail, 5, 3); 196 | InsertAtPosition(head, tail, 55, 1); 197 | InsertAtPosition(head, tail, 999, 50); 198 | InsertAtTail(tail, 600); 199 | Print(head); 200 | 201 | Node * mergeS = MergeSort(head); 202 | Print(head); 203 | Print(mergeS); 204 | 205 | 206 | return 0; 207 | } -------------------------------------------------------------------------------- /450dsa/MiddleOfLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | Node* MiddleOfLinkedListUsingRecursion(Node* head , Node* secoundHead){ 109 | Node *temp1 = head; 110 | Node* temp2 = secoundHead; 111 | if(temp1 == NULL || temp1->next == NULL){ 112 | return temp2; 113 | } 114 | Node* middle = MiddleOfLinkedListUsingRecursion(temp1->next->next , temp2->next); 115 | return middle; 116 | } 117 | 118 | Node* MiddleOfLinkedListUsingIteration(Node *head){ 119 | Node *temp1 = head; 120 | Node* temp2 = head; 121 | while(temp1 != NULL && temp1->next != NULL){ 122 | temp1 = temp1->next->next; 123 | temp2 = temp2->next; 124 | } 125 | return temp2; 126 | } 127 | 128 | int main() 129 | { 130 | 131 | // create Nod 132 | Node *start = new Node(12); 133 | 134 | Node *head = start; 135 | Node *tail = start; 136 | 137 | // Insert At Head 138 | InsertAtHead(head, 29); 139 | InsertAtHead(head, 3); 140 | InsertAtHead(head, 1); 141 | InsertAtHead(head, 9); 142 | InsertAtTail(tail, 0); 143 | InsertAtTail(tail, 23); 144 | InsertAtPosition(head, tail, 5, 3); 145 | InsertAtPosition(head, tail, 55, 1); 146 | InsertAtPosition(head, tail, 999, 50); 147 | InsertAtTail(tail, 600); 148 | InsertAtTail(tail, 2200); 149 | Print(head); 150 | 151 | Node* middle = MiddleOfLinkedListUsingRecursion(head , head); 152 | cout<data<data< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | int RemoveNFromBack(Node *&head , int n , Node * prev=NULL){ 109 | Node *temp = head; 110 | if(temp == NULL){ 111 | return 1; 112 | } 113 | int pos = RemoveNFromBack(temp->next , n , temp); 114 | if(pos == n){ 115 | prev->next = temp->next; 116 | return pos+1; 117 | } 118 | if(pos > n){ 119 | return pos+1; 120 | } 121 | 122 | return pos+1; 123 | 124 | } 125 | 126 | int main() 127 | { 128 | 129 | Node *start = new Node(2); 130 | Node *head = start; 131 | Node *tail = start; 132 | InsertAtHead(head, 4); 133 | InsertAtHead(head, 6); 134 | InsertAtHead(head, 12); 135 | InsertAtHead(head, 3); 136 | InsertAtTail(tail, 0); 137 | Print(head); 138 | 139 | RemoveNFromBack(head , 4 ); 140 | Print(head); 141 | 142 | return 0; 143 | 144 | } 145 | 146 | -------------------------------------------------------------------------------- /450dsa/ReverseLinkedListInKGroups.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | Node* ReverseKGroup(Node *&head , int k){ 109 | if(head == NULL){ 110 | return NULL; 111 | } 112 | 113 | Node *prev = NULL; 114 | Node *curr = head; 115 | Node *next = NULL; 116 | int cnt = 0; 117 | while(cntnext; 119 | curr->next = prev; 120 | 121 | prev = curr; 122 | curr = next; 123 | cnt++; 124 | } 125 | if (next != NULL){ 126 | head->next = ReverseKGroup(next , k); 127 | } 128 | 129 | return prev; 130 | } 131 | 132 | 133 | int main() 134 | { 135 | 136 | // create Nod 137 | Node *start = new Node(12); 138 | 139 | Node *head = start; 140 | Node *tail = start; 141 | 142 | // Insert At Head 143 | InsertAtHead(head, 29); 144 | InsertAtHead(head, 3); 145 | InsertAtHead(head, 1); 146 | InsertAtHead(head, 9); 147 | InsertAtTail(tail, 0); 148 | InsertAtTail(tail, 23); 149 | InsertAtPosition(head, tail, 5, 3); 150 | InsertAtPosition(head, tail, 55, 1); 151 | InsertAtPosition(head, tail, 999, 50); 152 | Print(head); 153 | 154 | ReverseKGroup(head , 2); 155 | Print(head); 156 | 157 | return 0; 158 | } -------------------------------------------------------------------------------- /450dsa/RotateALinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | int getLength(Node *&head){ 109 | int cnt = 0; 110 | Node* temp = head; 111 | while(temp!=NULL){ 112 | temp = temp->next; 113 | cnt++; 114 | } 115 | return cnt; 116 | } 117 | 118 | void RotateALinkedList(Node* &head , int k){ 119 | int size = getLength(head); 120 | if(k == size || k == 0){ 121 | return; 122 | } 123 | if(k > size){ 124 | k = k%size; 125 | } 126 | Node *temp = head; 127 | int remain = size - k; 128 | while(remain != 1){ 129 | temp = temp->next; 130 | remain--; 131 | 132 | } 133 | Node *head2 = temp->next; 134 | Node *temp2 = temp->next; 135 | temp->next = NULL; 136 | 137 | while(temp2->next != NULL){ 138 | temp2 = temp2->next; 139 | } 140 | temp2->next = head; 141 | head = head2; 142 | } 143 | 144 | int main() 145 | { 146 | 147 | Node *start = new Node(8); 148 | Node *head = start; 149 | Node *tail = start; 150 | InsertAtTail(tail, 9); 151 | InsertAtTail(tail, 3); 152 | InsertAtTail(tail, 13); 153 | InsertAtTail(tail, 4); 154 | InsertAtTail(tail, 2); 155 | InsertAtTail(tail, 1); 156 | Print(head); 157 | 158 | cout<<"Size->"< 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | void Print(Node *&head) 98 | { 99 | Node *temp = head; 100 | while (temp->next != NULL) 101 | { 102 | cout << temp->data << "->"; 103 | temp = temp->next; 104 | } 105 | cout << temp->data << "->NULL" << endl; 106 | } 107 | 108 | Node *ReverseLinkedListUsingIteration(Node* &head) 109 | { 110 | // Reverse An Linked List 111 | Node *prev = NULL; 112 | Node *curr = head; 113 | Node *next; 114 | 115 | while (curr != NULL) 116 | { 117 | next = curr->next; 118 | curr->next = prev; 119 | 120 | prev = curr; 121 | curr = next; 122 | } 123 | return prev; 124 | } 125 | 126 | Node *ReverseLinkedListUsingRecursion(Node* &h){ 127 | 128 | if(h == NULL || h->next == NULL){ 129 | return h; 130 | } 131 | 132 | Node * newHead = ReverseLinkedListUsingRecursion(h->next); 133 | h->next->next = h; 134 | h->next = NULL; 135 | 136 | return newHead; 137 | } 138 | 139 | void SecoundApproachRecursionReverse(Node* &head , Node *curr , Node*prev){ 140 | if(curr == NULL){ 141 | head = prev; 142 | return ; 143 | } 144 | SecoundApproachRecursionReverse(head , curr->next , curr); 145 | curr->next = prev; 146 | return; 147 | } 148 | 149 | int main() 150 | { 151 | 152 | // create Nod 153 | Node *start = new Node(12); 154 | 155 | Node *head = start; 156 | Node *tail = start; 157 | 158 | // Insert At Head 159 | InsertAtHead(head, 29); 160 | InsertAtHead(head, 3); 161 | InsertAtHead(head, 1); 162 | InsertAtHead(head, 9); 163 | InsertAtTail(tail, 0); 164 | InsertAtTail(tail, 23); 165 | InsertAtPosition(head, tail, 5, 3); 166 | InsertAtPosition(head, tail, 55, 1); 167 | InsertAtPosition(head, tail, 999, 50); 168 | InsertAtTail(tail, 600); 169 | Print(head); 170 | 171 | // Node *reverseLinkedListHeadRecursion = ReverseLinkedListUsingRecursion(head); 172 | // Print(reverseLinkedListHeadRecursion); 173 | 174 | 175 | // Node* reverseLinkedListHeadIteration = ReverseLinkedListUsingIteration(head); 176 | // Print(reverseLinkedListHeadIteration); 177 | 178 | SecoundApproachRecursionReverse(head , head , NULL); 179 | Print(head); 180 | 181 | return 0; 182 | } -------------------------------------------------------------------------------- /450dsa/floydCycleDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node *&tail, int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if (curr->next == NULL) 86 | { 87 | tail = prev; 88 | } 89 | cnt++; 90 | } 91 | prev->next = curr->next; 92 | curr->next = NULL; 93 | delete curr; 94 | } 95 | } 96 | 97 | bool detectCycle(Node *&head){ 98 | Node * slow = head; 99 | Node * fast = head; 100 | while(fast != NULL && fast->next != NULL){ 101 | slow = slow->next; 102 | fast=fast->next->next; 103 | 104 | if(fast == slow){ 105 | return true; 106 | } 107 | } 108 | return false; 109 | } 110 | 111 | Node * DetectStart(Node *& head){ 112 | Node * slow = head; 113 | Node * fast = head; 114 | while(fast != NULL && fast->next != NULL){ 115 | slow = slow->next; 116 | fast=fast->next->next; 117 | 118 | if(fast == slow){ 119 | break; 120 | } 121 | } 122 | if (fast == NULL || fast->next == NULL){ 123 | return NULL; 124 | } 125 | fast = head; 126 | while(slow->next == fast->next){ 127 | slow = slow->next; 128 | fast = fast->next; 129 | } 130 | return slow->next; 131 | 132 | } 133 | void Print(Node *&head) 134 | { 135 | Node *temp = head; 136 | while (temp->next != NULL) 137 | { 138 | cout << temp->data << "->"; 139 | temp = temp->next; 140 | } 141 | cout << temp->data << "->NULL" << endl; 142 | } 143 | 144 | Node *FloydCycleDetection(Node *&head) 145 | { 146 | if (head == NULL) 147 | { 148 | return head; 149 | } 150 | Node *slow = head; 151 | Node *fast = head->next; 152 | 153 | while (fast != NULL && slow != NULL) 154 | { 155 | slow = slow->next; 156 | fast = fast->next; 157 | if (fast != NULL) 158 | { 159 | fast = fast->next; 160 | } 161 | if (slow == fast) 162 | { 163 | return slow; 164 | } 165 | } 166 | 167 | return NULL; 168 | } 169 | 170 | 171 | int main() 172 | { 173 | 174 | // create Nod 175 | Node *start = new Node(12); 176 | 177 | Node *head = start; 178 | Node *tail = start; 179 | 180 | // Insert At Head 181 | InsertAtHead(head, 29); 182 | InsertAtHead(head, 3); 183 | InsertAtHead(head, 1); 184 | InsertAtHead(head, 9); 185 | InsertAtTail(tail, 34); 186 | InsertAtTail(tail, 600); 187 | InsertAtTail(tail, 900); 188 | InsertAtTail(tail, 100); 189 | InsertAtHead(head, 11); 190 | Print(head); 191 | 192 | cout << FloydCycleDetection(head)<< endl; 193 | 194 | // Make Circular 195 | tail->next = head->next->next; 196 | cout<next->data<next = NULL; 203 | 204 | Print(head); 205 | 206 | return 0; 207 | } -------------------------------------------------------------------------------- /450dsa/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | 2 | return true; -------------------------------------------------------------------------------- /C100BitGivenNumIsPowerOf2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPowerOf2(int num){ 5 | // BCD num ... 32 16 8 4 2 1 6 | // these are the power of 2 7 | // by seeing these numbers we see that that the the & binary operation by n & n-1 is always 0 in case f numbers which is power of 2 8 | // we ignore case when number is 0 9 | // if num is 0 then easily return 0 10 | return (num && !(num&(num-1))); 11 | } 12 | int main(){ 13 | // Write a Program to check gitven number ids Power of 2? 14 | 15 | // # 1 represent number is power of 2 16 | 17 | cout< 2 | 3 | using namespace std; 4 | 5 | // Bit manipulation get a Particular Bit 6 | // we left shift the 1 by the position you want to find and then & with number 7 | // if the output is not equsl to 0 then 1 bit is present at the place 8 | int getBit(int num , int pos){ 9 | return ((num & (1< 2 | 3 | using namespace std; 4 | 5 | int countOne(int num){ 6 | int times = 0; 7 | while (num != 0){ 8 | num = num & (num-1); 9 | times ++ ; 10 | } 11 | return times; 12 | } 13 | 14 | int main(){ 15 | // Write a program to count the number of ones in binary representation of a number. 16 | 17 | cout< 2 | 3 | using namespace std; 4 | 5 | void genrateAllSubset(char arr[] , int n){ 6 | // total subset of a set s consist of total no. of element n is 2^n-1 7 | int totalSubset = (1< 2 | using namespace std; 3 | 4 | #define n 100 5 | 6 | class Stack{ 7 | int *arr; 8 | int top; 9 | public : 10 | Stack(){ 11 | this->arr = new int[n]; 12 | this->top=-1; 13 | } 14 | 15 | void push(int x){ 16 | if(top == n-1){ 17 | cout<<"StackOverflow"<"< "< 2 | #include 3 | using namespace std; 4 | 5 | void ReverseString(string s){ 6 | stack st; 7 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | void InsertAtBottom(stack &st , int element){ 6 | if(st.empty()){ 7 | st.push(element); 8 | return; 9 | } 10 | int ele = st.top(); 11 | st.pop(); 12 | InsertAtBottom(st , element); 13 | st.push(ele); 14 | } 15 | 16 | void ReverseStack(stack &st){ 17 | if(st.empty()){ 18 | return; 19 | } 20 | int element = st.top(); 21 | st.pop(); 22 | ReverseStack(st); 23 | InsertAtBottom(st , element); 24 | } 25 | 26 | int main(){ 27 | stack st; 28 | st.push(2); 29 | st.push(4); 30 | st.push(7); 31 | st.push(6); 32 | 33 | ReverseStack(st); 34 | while(!st.empty()){ 35 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int prefixEvaluation(string str1){ 8 | stack st; 9 | for(int i = str1.length()-1; i>=0; i--){ 10 | if(str1[i] >= '0' && str1[i]<= '9'){ 11 | st.push(str1[i]-'0'); 12 | } 13 | else{ 14 | int opt1 = st.top(); 15 | st.pop(); 16 | int opt2 = st.top(); 17 | st.pop(); 18 | 19 | switch(str1[i]){ 20 | case '+': 21 | st.push(opt1 + opt2); 22 | break; 23 | 24 | case '-': 25 | st.push(opt1 - opt2); 26 | break; 27 | 28 | case '*': 29 | st.push(opt1 * opt2); 30 | break; 31 | 32 | case '/': 33 | st.push(opt1 / opt2); 34 | break; 35 | 36 | case '^': 37 | st.push(pow(opt1 , opt2)); 38 | break; 39 | } 40 | } 41 | } 42 | return st.top(); 43 | } 44 | 45 | 46 | int postfixEvaluation(string str1){ 47 | stack st; 48 | for(int i =0; i<=str1.length()-1; i++){ 49 | if(str1[i] >= '0' && str1[i]<= '9'){ 50 | st.push(str1[i]-'0'); 51 | } 52 | else{ 53 | int opt2 = st.top(); 54 | st.pop(); 55 | int opt1 = st.top(); 56 | st.pop(); 57 | 58 | switch(str1[i]){ 59 | case '+': 60 | st.push(opt1 + opt2); 61 | break; 62 | 63 | case '-': 64 | st.push(opt1 - opt2); 65 | break; 66 | 67 | case '*': 68 | st.push(opt1 * opt2); 69 | break; 70 | 71 | case '/': 72 | st.push(opt1 / opt2); 73 | break; 74 | 75 | case '^': 76 | st.push(pow(opt1 , opt2)); 77 | break; 78 | } 79 | } 80 | } 81 | return st.top(); 82 | } 83 | 84 | 85 | int main(){ 86 | string str1 = "-+7*45+20"; 87 | cout<<"Prefix->"<"< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int checkPrec(char c) 8 | { 9 | if (c == '^') 10 | { 11 | return 3; 12 | } 13 | else if (c == '*' || c == '/') 14 | { 15 | return 2; 16 | } 17 | else if (c == '+' || c == '-') 18 | { 19 | return 1; 20 | } 21 | else 22 | { 23 | return -1; 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | 30 | stack st; 31 | 32 | string infixString = "(a-b/c)*(a/k-l)"; 33 | // cin>>infixString; 34 | 35 | string res = ""; 36 | 37 | for (int i = 0; i < infixString.length(); i++) 38 | { 39 | if ((infixString[i] >= '0' && infixString[i] <= '9') || (infixString[i] >= 'a' && infixString[i] <= 'z') || (infixString[i] >= 'A' && infixString[i] <= 'Z')) 40 | { 41 | res += infixString[i]; 42 | } 43 | else if (infixString[i] == '(') 44 | { 45 | st.push(infixString[i]); 46 | } 47 | else if (infixString[i] == ')') 48 | { 49 | while (st.top() != '(') 50 | { 51 | res += st.top(); 52 | st.pop(); 53 | } 54 | st.pop(); 55 | } 56 | else 57 | { 58 | int precOp = checkPrec(infixString[i]); 59 | while (!st.empty() && checkPrec(st.top()) > precOp) 60 | { 61 | res += st.top(); 62 | st.pop(); 63 | } 64 | st.push(infixString[i]); 65 | } 66 | } 67 | while (!st.empty()) 68 | { 69 | res+=st.top(); 70 | st.pop(); 71 | } 72 | 73 | cout << res << endl; 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /C108InfixToPrefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int checkPrec(char c) 8 | { 9 | if (c == '^') 10 | { 11 | return 3; 12 | } 13 | else if (c == '*' || c == '/') 14 | { 15 | return 2; 16 | } 17 | else if (c == '+' || c == '-') 18 | { 19 | return 1; 20 | } 21 | else 22 | { 23 | return -1; 24 | } 25 | } 26 | 27 | int main() 28 | { 29 | 30 | stack st; 31 | 32 | string infixString = "(a-b/c)*(a/k-l)"; 33 | // cin>>infixString;. 34 | 35 | string prefixres = ""; 36 | 37 | // step1 : reverse the string and change the bracket 38 | // ( -> ) 39 | // ) -> ( 40 | 41 | // change bracket 42 | for(int i = 0; i= '0' && infixString[i] <= '9') || (infixString[i] >= 'a' && infixString[i] <= 'z') || (infixString[i] >= 'A' && infixString[i] <= 'Z')) 55 | { 56 | prefixres += infixString[i]; 57 | } 58 | else if (infixString[i] == '(') 59 | { 60 | st.push(infixString[i]); 61 | } 62 | else if (infixString[i] == ')') 63 | { 64 | while (st.top() != '(') 65 | { 66 | prefixres += st.top(); 67 | st.pop(); 68 | } 69 | st.pop(); 70 | } 71 | else 72 | { 73 | int precOp = checkPrec(infixString[i]); 74 | while (!st.empty() && checkPrec(st.top()) > precOp) 75 | { 76 | prefixres += st.top(); 77 | st.pop(); 78 | } 79 | st.push(infixString[i]); 80 | } 81 | } 82 | while (!st.empty()) 83 | { 84 | prefixres+=st.top(); 85 | st.pop(); 86 | } 87 | reverse(prefixres.begin() , prefixres.end()); 88 | cout << prefixres << endl; 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /C109BalanceParanthesis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | string str1; 8 | cin>>str1; 9 | bool ans = true; 10 | 11 | stack st; 12 | for(int i=0 ;i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Ternary Operator 8 | int a = 10; 9 | int b = 20; 10 | 11 | // excute first when true and execute second if false 12 | 13 | int c = a > b ? a : b; 14 | cout< 2 | using namespace std; 3 | 4 | #define n 20 5 | 6 | class queue{ 7 | int* arr; 8 | int front; 9 | int back; 10 | 11 | public: 12 | queue(){ 13 | arr = new int[n]; 14 | front = -1; 15 | back = -1; 16 | } 17 | 18 | void push(int x){ 19 | if(back == n-1){ 20 | cout<<"Queue Overflow"< back){ 33 | cout<<"No Element at Queue"< back){ 41 | cout<<"No Element at Queue"< back){ 49 | return true; 50 | } 51 | else{ 52 | return false; 53 | } 54 | } 55 | }; 56 | 57 | int main(){ 58 | 59 | queue q; 60 | q.push(1); 61 | q.push(2); 62 | q.push(3); 63 | q.push(4); 64 | 65 | cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node *next; 8 | Node(int data){ 9 | this->data = data; 10 | this->next = NULL; 11 | } 12 | }; 13 | 14 | class Queue{ 15 | Node *front; 16 | Node *back; 17 | 18 | public: 19 | Queue(){ 20 | Node *temp = new Node(1); 21 | this->front = temp; 22 | this->back = temp; 23 | } 24 | 25 | void push(int data){ 26 | Node *temp = new Node(data); 27 | back->next = temp; 28 | back = temp; 29 | } 30 | 31 | void pop(){ 32 | if(front->next != NULL){ 33 | front->next = front->next->next; 34 | } 35 | } 36 | 37 | int peek(){ 38 | if (front->next!= NULL){ 39 | return front->next->data; 40 | } 41 | return -1; 42 | } 43 | bool isEmpty(){ 44 | if (front->next == NULL){ 45 | return true; 46 | } 47 | return false; 48 | } 49 | 50 | }; 51 | 52 | int main(){ 53 | Queue q1; 54 | 55 | q1.push(1); 56 | q1.push(2); 57 | q1.push(3); 58 | q1.push(4); 59 | 60 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Queue{ 7 | stack s1; 8 | stack s2; 9 | 10 | public : 11 | void push(int data){ 12 | this->s1.push(data); 13 | } 14 | 15 | void pop(){ 16 | if(s1.empty() && s2.empty()){ 17 | cout<<"Queue is Empty"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Queue{ 7 | stack s1; 8 | public: 9 | void push(int data){ 10 | s1.push(data); 11 | } 12 | 13 | void pop(){ 14 | if(s1.empty()){ 15 | cout<<"Underflow"<"<"< 2 | using namespace std; 3 | 4 | class Stack{ 5 | queue q1; 6 | queue q2; 7 | int n = 0; 8 | public: 9 | void push(int data){ 10 | q2.push(data); 11 | n++; 12 | while(!q1.empty()){ 13 | q2.push(q1.front()); 14 | q1.pop(); 15 | } 16 | // set q1 , q2 = q2 , q1 17 | queue temp = q1; 18 | q1 = q2; 19 | q2 = temp; 20 | } 21 | 22 | void pop(){ 23 | if(n==0){ 24 | cout<<"Underflow"< q1; 50 | queue q2; 51 | int n = 0; 52 | public: 53 | void push(int data){ 54 | q1.push(data); 55 | n++; 56 | } 57 | 58 | void pop(){ 59 | if(n==0){ 60 | cout<<"Underflow"< temp = q1; 74 | q1 = q2; 75 | q2 = temp; 76 | } 77 | 78 | int top(){ 79 | if(n==0){ 80 | cout<<"Underflow"<"<"<"<"< 2 | #include 3 | using namespace std; 4 | 5 | void InsertAtBottom(stack &temp , int data){ 6 | if(temp.empty()){ 7 | temp.push(data); 8 | return; 9 | } 10 | int ele = temp.top(); 11 | temp.pop(); 12 | InsertAtBottom(temp , data); 13 | temp.push(ele); 14 | } 15 | 16 | void ReverserStack(stack &temp){ 17 | if(temp.empty()){ 18 | return; 19 | } 20 | int ele = temp.top(); 21 | temp.pop(); 22 | ReverserStack(temp); 23 | InsertAtBottom(temp , ele); 24 | } 25 | 26 | int main(){ 27 | stack q1 ; 28 | q1.push(1); 29 | q1.push(2); 30 | q1.push(3); 31 | q1.push(4); 32 | q1.push(5); 33 | 34 | cout<"< 2 | #include 3 | using namespace std; 4 | 5 | void InsertAtSortedPosition(stack &temp ,int data){ 6 | if(temp.empty()){ 7 | temp.push(data); 8 | return; 9 | } 10 | if(temp.top() < data){ 11 | temp.push(data); 12 | return; 13 | } 14 | int ele = temp.top(); 15 | temp.pop(); 16 | InsertAtSortedPosition(temp , data); 17 | temp.push(ele); 18 | } 19 | 20 | void SortStack(stack &temp){ 21 | if(temp.empty()){ 22 | return; 23 | } 24 | int ele = temp.top(); 25 | temp.pop(); 26 | SortStack(temp); 27 | InsertAtSortedPosition(temp , ele); 28 | } 29 | 30 | int main(){ 31 | stack q1; 32 | 33 | q1.push(-8); 34 | q1.push(1); 35 | q1.push(2); 36 | q1.push(-1); 37 | q1.push(20); 38 | 39 | cout<<"---Sorted Stack---"< 2 | using namespace std; 3 | 4 | void print(vector v){ 5 | for (int i=0; i findNextSmallest(vector v1 , int size){ 12 | vector ans(size); 13 | stack s1; 14 | s1.push(-1); 15 | for (int i=size-1; i>=0; i--){ 16 | int cur = v1[i]; 17 | while(cur <= s1.top()){ 18 | s1.pop(); 19 | } 20 | ans[i] = s1.top(); 21 | s1.push(cur); 22 | } 23 | return ans; 24 | } 25 | 26 | int main(){ 27 | vector a1; 28 | a1.push_back(13); 29 | a1.push_back(2); 30 | a1.push_back(4); 31 | a1.push_back(1); 32 | a1.push_back(12); 33 | a1.push_back(6); 34 | print(a1); 35 | 36 | // findNextSmallest 37 | print(findNextSmallest(a1 , a1.size())); 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /C11ReferenceOperator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // check location of a variable by & 8 | int a = 50; 9 | cout<<&(a); // return location in memory in hexadecimal form 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /C12PatternPrintRectangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Print Rectangle 8 | 9 | int col , row; 10 | cout<<"Enter the No. of Rows : "; 11 | cin>>row; 12 | cout<<"Enter the No. of Columns : "; 13 | cin>>col; 14 | 15 | for (int i = 1; i<=row ; i++){ 16 | for (int j = 1; j<=col ; j++){ 17 | cout<<" * "; 18 | } 19 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Print Hollow Rectangle 8 | 9 | int col , row; 10 | cout<<"Enter the No. of Rows : "; 11 | cin>>row; 12 | cout<<"Enter the No. of Columns : "; 13 | cin>>col; 14 | 15 | for (int i = 1; i<=row; i++){ 16 | for (int j = 1; j<=col; j++){ 17 | if (i == 1 || i == row){ 18 | cout<<" * "; 19 | } 20 | else{ 21 | if(j==1 || j==col){ 22 | cout<<" * "; 23 | } 24 | else{ 25 | cout<<" "; 26 | } 27 | } 28 | } 29 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Inverted HAlf Piramid 8 | 9 | int n; 10 | cout<<"N : "; 11 | cin>>n; 12 | 13 | for(int i = n; i>=1; i--){ 14 | //cout<<"*"; 15 | for(int j=1; j<=i; j++){ 16 | cout<<" * "; 17 | } 18 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Half Pyramiod Afteer 180 Rotation 8 | 9 | int n; 10 | cout<<"N : "; 11 | cin>>n; 12 | cout<=1; j--){ 16 | if(j<=i){ 17 | cout<<" * "; 18 | } 19 | else{ 20 | cout<<" "; 21 | } 22 | } 23 | cout<=j){ 31 | cout<<" "; 32 | } 33 | else{ 34 | cout<<" * "; 35 | } 36 | } 37 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Half Pyramid Using Number 8 | int n; 9 | cout<<"N : "; 10 | cin>>n; 11 | 12 | for (int i = 1; i<=n; i++){ 13 | for(int j = 1; j<=i; j++) 14 | { 15 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Floyd's Triangle 8 | int n; 9 | cout<<"N : "; 10 | cin>>n; 11 | 12 | int count = 1; 13 | for (int i = 0; i<=n; i++){ 14 | for(int j = 0; j<=i; j++){ 15 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Butterfly Pattern 8 | int n; 9 | cout<<"N : "; 10 | cin>>n; 11 | 12 | /* 13 | * = no. of row ( print each side ) 14 | space = 2*n - 2* row_num 15 | row = i 16 | */ 17 | 18 | for(int i = 1; i<=n; i++){ 19 | 20 | // print in start 21 | for(int j = 1; j <= i; j++){ 22 | cout<<" * "; 23 | } 24 | 25 | // Print Space 26 | int space = 2*n - 2*i; 27 | for(int j = 1; j<=space; j++){ 28 | cout<<" "; 29 | } 30 | 31 | // print in last 32 | for(int j = 1; j <= i; j++){ 33 | cout<<" * "; 34 | } 35 | cout<=1; i--){ 40 | 41 | // print in start 42 | for(int j = 1; j <= i; j++){ 43 | cout<<" * "; 44 | } 45 | 46 | // Print Space 47 | int space = 2*n - 2*i; 48 | for(int j = 1; j<=space; j++){ 49 | cout<<" "; 50 | } 51 | 52 | // print in last 53 | for(int j = 1; j <= i; j++){ 54 | cout<<" * "; 55 | } 56 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Inverted Pattern 8 | int n; 9 | cout<<"N : "; 10 | cin>>n; 11 | 12 | for (int x = n; x>=1; x--){ 13 | for(int y = 1; y<=x; y++){ 14 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | cout << "Hello My NAme is Ved Prakash Gupta" << endl; 8 | return 0; 9 | } -------------------------------------------------------------------------------- /C20PatternPrint0_1Pattern.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // 0-1 Pattern 8 | int n; 9 | cout<<"N : "; 10 | cin>>n; 11 | bool b = 1; 12 | for(int i = 1; i<=n; i++){ 13 | for(int j = 1 ; j<=i; j++){ 14 | cout<<" "; 15 | if(i%2!=0){ 16 | if(j%2!=0){ 17 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // Rhombus Pattern 7 | int n; 8 | cout<<"N : "; 9 | cin>>n; 10 | 11 | for(int i = n; i>=1; i--){ 12 | 13 | // 1st loop for space 14 | for(int j = 1; j<=i-1; j++){ 15 | cout<<" "; 16 | } 17 | 18 | // 2nd loop for stars 19 | for(int sec = 1; sec<=n; sec++){ 20 | cout<<" * "; 21 | } 22 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Number Pattern 8 | cout<<" N : "; 9 | int n; 10 | cin>>n; 11 | 12 | for(int i = 1; i<=n; i++){ 13 | 14 | // Add space 15 | for(int j = 0; j<=n-i+1; j++){ 16 | cout<<" "; 17 | } 18 | 19 | // Add number 20 | for(int j = 1; j<=i; j++){ 21 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Palindromic Pattern 8 | int n; 9 | cout<<"N : "; 10 | cin>>n; 11 | 12 | for(int i = 1; i<=n; i++ ){ 13 | for(int j = 1; j<=n-i; j++){ 14 | cout<<" "; 15 | } 16 | for(int k = i; k>=1; k--){ 17 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Star Pattern 8 | cout<<" N : "; 9 | int n; 10 | cin>>n; 11 | 12 | for(int i = 1; i<=n; i++){ 13 | for(int j = 1; j<=n-i; j++){ 14 | cout<<" "; 15 | } 16 | for(int j = 1; j<=2*i-1 ; j++){ 17 | cout<<" * "; 18 | } 19 | cout<=1; i--){ 23 | for(int j = 1; j<=n-i; j++){ 24 | cout<<" "; 25 | } 26 | for(int j = 1; j<=2*i-1 ; j++){ 27 | cout<<" * "; 28 | } 29 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Zig-Zag PAttern 8 | cout<<"N : "; 9 | int n; 10 | cin>>n; 11 | 12 | for(int i = 1; i<=3; i++){ 13 | for(int j =1; j<=n; j++){ 14 | if((i+j)%4==0){ 15 | cout<<"*"; 16 | } 17 | else if(i==2 && j%4==0){ 18 | cout<<" * "; 19 | } 20 | else{ 21 | cout<<" "; 22 | } 23 | } 24 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Check Number is Prime Or Not 9 | int n; 10 | cout<<"Enter The Number : "; 11 | cin>>n; 12 | bool flag = 0; 13 | 14 | // we check from 2 to sqrt of n because if no. is non prime its already covers its this 15 | for(int x = 2; x < sqrt(n); x++){ 16 | if(n%x==0){ 17 | cout<<"\nNumber is Non Prime"; 18 | flag = 1; 19 | break; 20 | } 21 | 22 | } 23 | if (flag!=1){ 24 | cout<<"\nNumber is Prime"; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /C27ReverseANumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Reverse a Number 8 | // cout<<"Enter the Number to Reverse : "; 9 | int n; 10 | cin>>n; 11 | 12 | int reverse = 0; 13 | while(n>0){ 14 | int last = n%10; 15 | reverse = reverse*10 + last; 16 | 17 | // remove last digit from n 18 | n = n/10; 19 | } 20 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // Check Number is Armstrong or Not 9 | int N , n; 10 | cout<<"Enter the NUmber : "; 11 | cin>>N; 12 | 13 | int sq_sum = 0; 14 | n = N; 15 | 16 | while(n>0){ 17 | int lastdigit = n%10; 18 | 19 | // sum of square of digits 20 | //sq_sum = sq_sum + (lastdigit*lastdigit*lastdigit); 21 | sq_sum += pow(lastdigit , 3); 22 | // cout< 2 | 3 | using namespace std; 4 | 5 | // Add function 6 | int add(int num1 , int num2){ 7 | int sum = num1 + num2; 8 | return sum; 9 | } 10 | int main(){ 11 | 12 | // function for addition 13 | cout<<"Enter two Numbers for Addition : "; 14 | int a , b; 15 | cin>>a>>b; 16 | int sum = add(a , b); 17 | cout<<"Addition is : "< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int a ; // declare 7 | a = 56; // initialisation 8 | cout<< "size of int : "<< sizeof(a)<<" byte" < 2 | #include 3 | 4 | using namespace std; 5 | 6 | bool isPrime(int n){ 7 | /* Function checks Number is Prime Or Not*/ 8 | bool flag = true; 9 | for(int x = 2; x<=sqrt(n); x++){ 10 | if(n%x==0){ 11 | flag = false; 12 | break; 13 | } 14 | } 15 | return flag; 16 | } 17 | 18 | 19 | int main() 20 | { 21 | // find prime number in given range 22 | int a , b; 23 | cout<<"Enter the Rangr : "; 24 | cin>>a>>b; 25 | for(int i = a; i<=b; i++){ 26 | if(isPrime(i) == true){ 27 | cout< 2 | 3 | using namespace std; 4 | 5 | void fab(int num){ 6 | int a = 0 , b = 1; 7 | for(int i = 1 ; i<=num; i++){ 8 | cout<>n; 21 | 22 | fab(n); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /C33FactorialUsingFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | /*Factorial of an Number*/ 7 | int fact = 1; 8 | for(int i = n; i>=1; i--){ 9 | fact *= i; 10 | } 11 | return fact; 12 | } 13 | int main(){ 14 | 15 | // find factorial of an number 16 | cout<<"Enter the Number : "; 17 | int num; 18 | cin>>num; 19 | 20 | int fact = factorial(num); 21 | cout<<"Factorial is : "< 2 | 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | /*Factorial of an Number*/ 7 | int fact = 1; 8 | for(int i = n; i>=1; i--){ 9 | fact *= i; 10 | } 11 | return fact; 12 | } 13 | int main(){ 14 | 15 | // Calculate Binary Cofficient nCr 16 | int n , r; 17 | cout<<" Enter n & r : "; 18 | cin>>n>>r; 19 | 20 | int nCr = factorial(n)/(factorial(n-r)*factorial(r)); 21 | cout<<"nCr : "< 2 | 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | /*Factorial of an Number*/ 7 | int fact = 1; 8 | for(int i = n; i>=1; i--){ 9 | fact *= i; 10 | } 11 | return fact; 12 | } 13 | 14 | int nCr(int n , int r){ 15 | /* find Binary Cofficient nCr */ 16 | int total = factorial(n)/(factorial(n-r)*factorial(r)); 17 | return total; 18 | } 19 | 20 | int main() 21 | { 22 | // Pascal Triangle 23 | cout<<"N : "; 24 | int n; 25 | cin>>n; 26 | 27 | for(int i = 0; i 2 | 3 | using namespace std; 4 | 5 | int sum(int n){ 6 | /* For loop Approch*/ 7 | int s = 0; 8 | for(int i = 1; i<=n; i++){ 9 | s+=i; 10 | 11 | } 12 | return s; 13 | } 14 | 15 | int sum_formula(int n){ 16 | int sum; 17 | sum = (n*(n+1))/2; 18 | return sum; 19 | } 20 | 21 | int32_t main(){ 22 | // Sum of N natural Number 23 | 24 | int n; 25 | cin>>n; 26 | 27 | cout< 2 | 3 | using namespace std; 4 | 5 | bool check(int x , int y , int z){ 6 | int a ,b ,c; 7 | a = max(x , max(y , z)); 8 | 9 | if(a==x){ 10 | b = y; 11 | c = z; 12 | } 13 | else if(a==y){ 14 | b = x; 15 | c = z; 16 | } 17 | else{ 18 | b = x; 19 | c = y; 20 | } 21 | 22 | if(a*a == b*b + c*c) 23 | return true; 24 | else 25 | return false; 26 | } 27 | 28 | int32_t main(){ 29 | int x , y , z; 30 | 31 | // check the Number is in the form of Pythagorean Triplet 32 | cin>>x>>y>>z; 33 | 34 | if(check(x,y,z)) 35 | { 36 | cout<<"Pythagorean Triplet"; 37 | } 38 | else{ 39 | cout<<"Not a Pythagorean Triplet"; 40 | } 41 | } -------------------------------------------------------------------------------- /C38BinaryToDecimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int B2D(int n){ 6 | int decimal = 0; 7 | int temp = 1; // we represent temp as a power of 2 sequence 8 | while (n>0){ 9 | int last_digit = n%10; 10 | n = n/10; 11 | decimal += last_digit*temp; 12 | temp *= 2 ; // change into its next power 13 | 14 | } 15 | return decimal; 16 | } 17 | 18 | int32_t main(){ 19 | // Binary to Decimal Conversion 20 | int n; 21 | cin>>n; 22 | 23 | cout<<"Binary to Decimal : "< 2 | 3 | using namespace std; 4 | 5 | int O2D(int n){ 6 | int decimal = 0; 7 | int temp = 1; 8 | 9 | while (n>0){ 10 | int last = n%10; 11 | n /= 10; 12 | decimal += last*temp; 13 | temp *= 8; 14 | } 15 | return decimal; 16 | } 17 | 18 | int32_t main(){ 19 | // Octal To Decimal 20 | int n; 21 | cin>>n; 22 | 23 | cout<<"Octal To Decimal : "< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // output standard 8 | std::cout<<"Hello i am Output"<>a; // enter var to store 13 | cin>>b; 14 | 15 | cout<<"Sum is : "< 2 | 3 | using namespace std; 4 | 5 | int H2D(string n){ 6 | int decimal = 0; 7 | int temp = 1; 8 | 9 | // check the size of input string 10 | int size = n.size(); 11 | // cout<=0; i--){ 13 | if(n[i] >= '0' && n[i] <='9'){ 14 | // if String index element is lie between 0 to 9 15 | decimal += temp*(n[i]-'0'); 16 | } 17 | else if(n[i] >= 'A' && n[i] <= 'F'){ 18 | // if String element in between A to F 19 | decimal += temp*(n[i] - 'A' +10); 20 | } 21 | temp*=16; 22 | } 23 | return decimal; 24 | } 25 | 26 | int32_t main(){ 27 | // Hexadecimal to Decimal 28 | // we knoew that hexadecial cotain Alphabets and NumbersSoo we input a String 29 | 30 | string n; 31 | cin>>n; 32 | 33 | cout<<"Hexadecimal to Decimal : "< 2 | 3 | using namespace std; 4 | 5 | int D2B(int n){ 6 | // check largest power of 2 7 | int x =1; //temporary variable 8 | int Binary = 0; // Output 9 | while(x<=n){ 10 | x*=2; // double each time 11 | } 12 | // if loop ended x value is bigger than n so we divide by 2 previous mulltiple of2 13 | x /= 2; 14 | 15 | 16 | while(x>0){ 17 | int binary_digit = n/x; // extract largest portion from n multiple of 2; 18 | n -= binary_digit*x; 19 | 20 | // then xcheck for previos multiple of 2 21 | x /= 2; 22 | Binary = Binary*10 + binary_digit; 23 | 24 | } 25 | return Binary; 26 | } 27 | 28 | int32_t main(){ 29 | //Decimal to Binary 30 | int n; 31 | cin>>n; 32 | 33 | cout<<"Decimal to Binary : "< 2 | 3 | using namespace std; 4 | 5 | int D2O(int n){ 6 | // check largest power of 2 7 | int x =1; //temporary variable 8 | int Octal = 0; // Output 9 | while(x<=n){ 10 | x*=8; // double each time 11 | } 12 | // if loop ended x value is bigger than n so we divide by 8 previous mulltiple of8 13 | x /= 8; 14 | 15 | 16 | while(x>0){ 17 | int Octal_digit = n/x; // extract largest portion from n multiple of 8; 18 | n -= Octal_digit*x; 19 | 20 | // then xcheck for previos multiple of 8 21 | x /= 8; 22 | Octal = Octal*10 + Octal_digit; 23 | 24 | } 25 | return Octal; 26 | 27 | } 28 | 29 | int32_t main(){ 30 | //Decimal to Octal 31 | int n; 32 | cin>>n; 33 | 34 | cout<<"Decimal to Octal : "< 2 | 3 | using namespace std; 4 | 5 | string D2H(int n){ 6 | // check largest power of 2 7 | int x =1; //temporary variable 8 | string hex = ""; // Output 9 | while(x<=n){ 10 | x*=16; // double each time 11 | } 12 | // if loop ended x value is bigger than n so we divide by 16 previous mulltiple of16 13 | x /= 16; 14 | 15 | 16 | while(x>0){ 17 | int hex_digit = n/x; // extract largest portion from n multiple of 16; 18 | n -= hex_digit*x; 19 | 20 | // then xcheck for previos multiple of 16 21 | x /= 16; 22 | if(hex_digit <= 9){ 23 | hex = hex + to_string(hex_digit); // Convert digit in to string and add to ans 24 | } 25 | else{ 26 | // if number is greater than 9 so convert it into A /B /C 27 | // And Add in the last of string 28 | char c = 'A' + hex_digit - 10; 29 | hex.push_back(c); 30 | } 31 | 32 | } 33 | return hex; 34 | 35 | } 36 | 37 | int32_t main(){ 38 | //Decimal to hex 39 | int n; 40 | cin>>n; 41 | 42 | cout<<"Decimal to hex : "< 2 | 3 | using namespace std; 4 | 5 | int check(int n){ 6 | // check highest power of 8 in n 7 | int x = 1; 8 | int ans = 0; 9 | while(x<=n){ 10 | x*=8; 11 | } 12 | x /= 8; 13 | 14 | while(n>0){ 15 | // extract last powers 16 | int last = n/x; 17 | n -= last*x; 18 | 19 | // reduce x for check previous power 20 | x /= 8; 21 | 22 | ans = ans*10 + last; 23 | } 24 | return ans; 25 | } 26 | 27 | int32_t main(){ 28 | // check 29 | int n ; 30 | cin>>n; 31 | 32 | cout< 2 | using namespace std; 3 | 4 | int reverse(int n){ 5 | int ans = 0; 6 | while(n>0){ 7 | int last = n%10; 8 | n /= 10; 9 | ans = ans*10 + last; 10 | } 11 | return ans; 12 | } 13 | int sumf(int a , int b){ 14 | int sum = 0; 15 | int carry = 0; 16 | while (a !=0 && b !=0){ 17 | int x = a%10; 18 | int y = b%10; 19 | if(x == 0 && y ==0 ){ 20 | sum = sum*10 + carry; 21 | carry = 0; 22 | } 23 | else if((x == 0 && y == 1) || (x == 1 && y == 0)){ 24 | // if any one is 1 25 | if(carry == 1){ 26 | sum = sum*10 + 0; 27 | carry = 1; // carry set to 1 28 | } 29 | else if(carry == 0){ 30 | sum = sum*10 + 1; 31 | carry = 0; 32 | } 33 | 34 | } 35 | else if (x == 1 && y == 1){ 36 | // if both are possitive 37 | 38 | if(carry == 1){ 39 | 40 | sum = sum*10 + 1; 41 | carry = 1; 42 | } 43 | else if(carry == 0){ 44 | 45 | sum = sum*10 + 0; 46 | carry = 1; 47 | 48 | } 49 | } 50 | // Update a and b 51 | a /= 10; 52 | b /= 10; 53 | 54 | } 55 | if (carry == 1){ 56 | // cout<<"yes"<>a>>b; 71 | 72 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | // Array 7 | int arr1[4] = {10,20,30,40}; 8 | // cout<>n; 14 | int arr2[n]; 15 | for(int i = 0; i < n; i++){ 16 | cout<<"\nEnter Element at "<>arr2[i]; 18 | } 19 | cout<<"\nArray Elements is : "; 20 | for(int i = 0; i < n; i++){ 21 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | // Max Min Element in Array 8 | 9 | cout<<"Enter No. of element in Array : "; 10 | 11 | int n; 12 | cin>>n; 13 | int arr2[n]; 14 | for(int i = 0; i < n; i++){ 15 | cin>>arr2[i]; 16 | } 17 | int max0 = INT_MIN; 18 | int min0 = INT_MAX; 19 | 20 | int max1 = INT_MIN; 21 | int min1 = INT_MAX; 22 | 23 | for (int i = 0; i max0){ 25 | max0 = arr2[i]; 26 | } 27 | if(arr2[i] < min0){ 28 | min0 = arr2[i]; 29 | } 30 | // we also use maxmin function that returns greater and smaller values in comparishion of two elements 31 | max1=max(arr2[i] , max1); 32 | min1=min(arr2[i] , min1); 33 | } 34 | cout<<"Max Element is : "< 2 | 3 | using namespace std; 4 | 5 | int linearSearch(int arr1[] , int n , int key){ 6 | /* Linear Searching in Array */ 7 | for(int i = 0; i>e; 24 | 25 | int l = linearSearch(arr1 , n , e); 26 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int BinarySearch(int arr[] , int n , int key){ 7 | /* Used in Sorted Array */ 8 | int startPosition = 0; 9 | int endPosition = n; 10 | while(startPosition <= endPosition){ 11 | int midPosition = (startPosition + endPosition)/2; // store mid index 12 | // cout< key){ 21 | endPosition = midPosition - 1; 22 | } 23 | // cout<>e; 37 | 38 | int l = BinarySearch(arr1 , n , e); 39 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Main Block 8 | int sav; 9 | cin>>sav; 10 | if(sav>5000){ 11 | cout<<"Saving is Greater than 5000 "; 12 | } 13 | else if(sav>2000){ 14 | cout<<"Saving is Greater than 2000 but less than 5000 "; 15 | } 16 | else{ 17 | cout<<"Saving is less than 2000 & "; 18 | if(sav!=0){ 19 | cout<<"Saving not equal to zero "; 20 | } 21 | else{ 22 | cout<<"Saving is zero"; 23 | } 24 | } 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /C50SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | // Selection Sort 7 | cout<<"Enter the Length of Array : "; 8 | int n; 9 | cin>>n; 10 | int arr1[n]; 11 | 12 | for(int i = 0; i>arr1[i]; 14 | } 15 | 16 | for(int x = 0; x arr1[y]){ 19 | int temp = arr1[y]; 20 | arr1[y] = arr1[x]; 21 | arr1[x] = temp; 22 | } 23 | } 24 | } 25 | cout<<"\nSorted Array is : "; 26 | for(int i = 0; i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | // Bubble Sort 7 | cout<<"Enter the Length of Array : "; 8 | int n; 9 | cin>>n; 10 | int arr1[n]; 11 | 12 | for(int i = 0; i>arr1[i]; 14 | } 15 | 16 | for(int x = 0; x arr1[i+1]){ 19 | int temp = arr1[i+1]; 20 | arr1[i+1] = arr1[i]; 21 | arr1[i] = temp; 22 | } 23 | } 24 | } 25 | 26 | cout<<"\nSorted Array is : "; 27 | for(int i = 0; i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | // Insertion Sort 7 | cout<<"Enter the Length of Array : "; 8 | int n; 9 | cin>>n; 10 | int arr1[n]; 11 | 12 | for(int i = 0; i>arr1[i]; 14 | } 15 | 16 | for(int i = 1; i=0 && arr1[j] > current){ 21 | arr1[j+1] = arr1[j]; 22 | j--; 23 | } 24 | arr1[j+1] = current; 25 | } 26 | 27 | 28 | cout<<"\nSorted Array is : "; 29 | for(int i = 0; i 2 | #include 3 | 4 | using namespace std; 5 | int main(){ 6 | // Max Till I 7 | int n; 8 | cout<<"Number of Elements in Array : "; 9 | cin>>n; 10 | int arr[n]; 11 | 12 | for(int i = 0; i>arr[i]; 14 | } 15 | int max1 = INT_MIN; 16 | for(int i = 0; i 2 | #include 3 | 4 | using namespace std; 5 | int main(){ 6 | // Sum of All SubArrays 7 | int n; 8 | cout<<"Number of Elements in Array : "; 9 | cin>>n; 10 | int arr[n]; 11 | 12 | for(int i = 0; i>arr[i]; 14 | } 15 | 16 | for(int i = 0; i 2 | 3 | using namespace std; 4 | 5 | int32_t main(){ 6 | // find longest Arithmetic Subarray 7 | 8 | int n = 7; 9 | int arr[8] = {10,7,4,6,8,10,11}; 10 | 11 | int max_diff = 0; 12 | 13 | int diff; 14 | int count = 2; // Start with 2 if no difference are equal then subarray size is also true 15 | for(int i=1; i 2 | 3 | using namespace std; 4 | 5 | int32_t main(){ 6 | // Recorrd Breaker 7 | /* 8 | 1. Strictly Greater than all previous Day 9 | 2. Strictly greater than following value -> greater than next value 10 | 11 | Constraints : 12 | 20 secounds 13 | 1GB limit 14 | test case 1: 1<=N<=1000 15 | test case 2: 1<=N<=2*10^5 16 | 17 | our solution is O(n) or O(log n) or O(n^1/2) 18 | */ 19 | int n; 20 | cin>>n; 21 | int arr[n]; 22 | // int arr[8] = {1,2,0,7,2,0,2,2}; 23 | 24 | for(int i = 0; i>arr[i]; 26 | } 27 | 28 | // if only one no. is in array 29 | if (n == 1){ 30 | cout<<"1"< arr[i+1]){ 40 | recordBreakingDay += 1; 41 | // cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // First Repeating Element 6 | int n; 7 | cin>>n; 8 | int arr[n]; 9 | int maxe = INT_MIN; // max element in array 10 | for(int i = 0; i>arr[i]; 12 | maxe = max(maxe , arr[i]); 13 | } 14 | // cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // find start and end of Subarray with given sum 6 | // two pointers approch 7 | int n ,s; 8 | cin>>n>>s; 9 | int arr[n]; 10 | for(int i=0; i>arr[i]; 12 | } 13 | 14 | int start = 0; 15 | int end = 0; 16 | int sum = 0; 17 | 18 | int i =0; 19 | // cout<<"yes"; 20 | while(sum != s && end<=n){ 21 | // cout< s){ 23 | start += 1; 24 | end = start; 25 | sum = 0; 26 | } 27 | sum = sum+arr[end]; 28 | end += 1; 29 | // cout<n){ 31 | cout<<"Subarray not available"; 32 | return 0; 33 | } 34 | } 35 | cout<<"Start : "< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Smallest Positive Missing Number 6 | int n; 7 | cin>>n; 8 | 9 | int arr[n]; 10 | int sum = 0; 11 | for(int i = 0; i>arr[i]; 13 | sum = max(sum , arr[i]); 14 | } 15 | 16 | // make a boolean array that store 1 in index if index number is present 17 | // boolean array of highest size of positive integer 18 | bool arr_index[sum]; 19 | for(int i=0; i=0){ 25 | arr_index[arr[i]] = true; 26 | } 27 | } 28 | 29 | // check for smallest integer 30 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int a,b,c; 8 | 9 | cin>>a>>b>>c; 10 | 11 | // print input 12 | // cout<b){ 15 | if(a>c){ 16 | cout<<"A is Greater than B and C"; 17 | } 18 | else{ 19 | cout<<"C is Greater than B and A"; 20 | } 21 | } 22 | else if(b>c){ 23 | cout<<"B is Greater than A and C"; 24 | } 25 | else{ 26 | cout<<"C is Greater tha A and B"; 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /C60PrintAllSubarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | int n; 8 | cin>>n; 9 | 10 | int arr[n]; 11 | // Input an Array 12 | for(int i=0; i>arr[i]; 14 | } 15 | 16 | // Print All Possible SubArray 17 | int start = 0 , end = 0; 18 | while (end=n){ 24 | start += 1; 25 | end = start; 26 | } 27 | cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | 6 | int n; 7 | cin>>n; 8 | 9 | int arr[n]; 10 | for(int i=0; i>arr[i]; 12 | } 13 | 14 | for(int i=0; i<=n; i++){ 15 | for(int j = i; j 2 | 3 | using namespace std; 4 | 5 | int32_t main(){ 6 | // Print SubArray Which Has Maximum Sum 7 | int n; 8 | cin>>n; 9 | 10 | int arr[n]; 11 | for(int i = 0; i>arr[i]; 13 | } 14 | 15 | // total subarray in size of N array id : N * (N + 1)/2. 16 | int t = (n *(n + 1))/2; 17 | // cout<= n){ 45 | start_ele += 1; 46 | end_ele = start_ele; 47 | } 48 | sum_ele = 0; 49 | } 50 | /* 51 | for(int i = 0; i 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Find Maximum Sub Array in Array 6 | int n; 7 | cin>>n; 8 | 9 | int arr1[n]; 10 | for(int i = 0; i>arr1[i]; 12 | } 13 | 14 | // ..............................0. 15 | int sum_subarray = 0; 16 | int max_subarray = INT_MIN; 17 | for(int i=0; i 2 | 3 | using namespace std; 4 | 5 | int32_t main(){ 6 | // Max Subarray sum by Cummulativee Sum 7 | int n; 8 | cin>>n; 9 | int arr[n]; 10 | for(int i=0;i>arr[i]; 12 | } 13 | 14 | int cumArray[n+1]; 15 | // Start with zero 16 | cumArray[0] = 0; 17 | for (int i = 1; i<=n; i++){ 18 | cumArray[i] = cumArray[i-1] + arr[i-1]; 19 | } 20 | 21 | int maxSum = INT_MIN; 22 | 23 | for(int i=1; i<=n; i++){ 24 | int localSum = 0; 25 | for(int j=0; j 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // kedans Algorithm 6 | int n; 7 | cin>>n; 8 | 9 | int arr1[n]; 10 | for(int i=0; i>arr1[i]; 12 | } 13 | 14 | int cur_sum = 0; 15 | int max_sum = 0; 16 | int neg = INT_MIN; 17 | bool flag = 0; 18 | bool flag_check = 0; 19 | 20 | 21 | for(int i=0; i max_sum){ 25 | max_sum = cur_sum; 26 | // cout< 2 | using namespace std; 3 | int32_t main(){ 4 | int n; 5 | cin>>n; 6 | int a[n]; 7 | for(int i = 0; i>a[i]; 9 | } 10 | 11 | //................................................. 12 | int max_ele = INT_MIN; 13 | int swap = 0; 14 | 15 | for(int i=0; i a[j+1]){ 18 | int temp = a[j]; 19 | a[j] = a[j+1]; 20 | a[j+1] = temp; 21 | swap++; 22 | } 23 | } 24 | } 25 | 26 | cout<<"Array is sorted in "< 2 | 3 | using namespace std; 4 | 5 | int32_t main(){ 6 | // Max Subarray sum by Cummulativee Sum 7 | int n; 8 | cin>>n; 9 | int arr[n]; 10 | for(int i=0;i>arr[i]; 12 | } 13 | 14 | int maxSum = INT_MIN; 15 | int curSum = 0; 16 | int neg = INT_MIN; 17 | bool flag = false; 18 | 19 | for(int i=0; i 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Maximum Circular SubArray Sum with Brute Force Approch 6 | 7 | int n; 8 | cin>>n; 9 | int arr[n]; 10 | for(int i=0;i>arr[i]; 12 | } 13 | // lets make kedans Algorithm 14 | int maxSum = INT_MIN; 15 | int cursum = 0; 16 | int start = 0; 17 | bool flag = false; 18 | 19 | for (int i=0; i 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | //Loops is Cpp 8 | 9 | // print 1 to 10 by for loop 10 | for (int i = 0; i<11; i++){ 11 | cout< 2 | using namespace std; 3 | 4 | int kadans(int arr[] , int n){ 5 | /*kadans Algoritm implementation*/ 6 | int maxSum = INT_MIN; 7 | int curSum = 0; 8 | 9 | for(int i=0; i>n; 24 | int arr[n]; 25 | int total=0; 26 | 27 | for(int i=0;i>arr[i]; 29 | total += arr[i]; 30 | } 31 | 32 | // cout< 2 | using namespace std; 3 | 4 | 5 | int32_t main(){ 6 | 7 | // Pair Sum Problem 8 | // try Brute force Approach 9 | /* 10 | Check if there exists two elements in an array such that their sum 11 | is equal to given k 12 | */ 13 | 14 | int n; 15 | cin>>n; 16 | 17 | int arr[n]; 18 | for(int i=0; i>arr[i]; 20 | } 21 | 22 | int k; 23 | cin>>k; 24 | 25 | bool flag = false; // set true when sum of pair is available 26 | 27 | for (int i=0; i 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | 6 | // Pair Sum Problem 7 | // try Optimal Approach 8 | /* 9 | Check if there exists two elements in an array such that their sum 10 | is equal to given k 11 | */ 12 | 13 | int n; 14 | cin>>n; 15 | 16 | int arr[n]; 17 | int maxele = INT_MIN; 18 | 19 | for(int i=0; i>arr[i]; 21 | maxele = max(maxele , arr[i]); 22 | } 23 | // cout<>k; // Sumpair element 35 | 36 | for(int i=0; i 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Pair Sum Problem Optimal Approch 6 | // Use in Sorted Array 7 | int n; // num 8 | cin>>n; 9 | 10 | int arr[n]; // Array 11 | for(int i=0; i>arr[i]; 13 | } 14 | 15 | int k; // sum 16 | cin>>k; 17 | 18 | int low = 0; 19 | int high = n-1; 20 | int flag = false; 21 | 22 | while(low != high && low k){ 24 | high--; 25 | } 26 | else if(arr[low] + arr[high] < k){ 27 | low++; 28 | } 29 | else{ 30 | cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | int n = 8; 6 | int arr[8] = {2,4,5,8,9,17,21,22}; // Sorted Array 7 | 8 | int k = 6; // Pair Sum you want to find 9 | 10 | int tempSum = 0; 11 | int low = 0; int high = n-1; 12 | 13 | bool flag = false; 14 | while (tempSum!=k && low k){ 16 | high--; 17 | } 18 | else if (arr[low] + arr[high] < k){ 19 | low ++; 20 | } 21 | else{ 22 | flag = true; 23 | break; 24 | } 25 | } 26 | cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Start with 2 Dimensionl Array 6 | int n , m; 7 | cin>>n>>m; 8 | int arr[n][m]; 9 | 10 | // Input an Array 11 | for(int i=0; i>arr[i][j]; 14 | } 15 | } 16 | 17 | // Output 18 | cout<<"2D Array is : "< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Searching 2 Dimensionl Array 6 | int n , m; 7 | cin>>n>>m; 8 | int arr[n][m]; 9 | 10 | // Input an Array 11 | for(int i=0; i>arr[i][j]; 14 | } 15 | } 16 | 17 | int k; 18 | cin>>k; // element you want to find 19 | int flag = false; 20 | 21 | for(int i=0; i 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Input 6 | int i , j; 7 | cin>>i>>j; 8 | 9 | int arr[i][j]; // Intialize 10 | 11 | for(int x=0; x>arr[x][y]; 14 | } 15 | } 16 | 17 | 18 | int r_start = 0; 19 | int r_end = i-1; 20 | 21 | int c_start = 0; 22 | int c_end = j-1; 23 | 24 | while(r_start <= r_end && c_start <= c_end){ 25 | 26 | for(int a = c_start; a <= c_end; a++){ 27 | cout<= c_start; c--){ 37 | cout<= r_start; d--){ 42 | cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | int n1=3 , n2=4 , n3=3; 6 | int arr1[3][4] = {{2,4,1,2}, 7 | {8,4,3,6}, 8 | {1,7,9,5}}; 9 | 10 | int arr2[4][3] = {{1,2,3}, 11 | {4,5,6}, 12 | {7,8,9}, 13 | {4,5,6}}; 14 | 15 | int arrOut[n1][n3]; //Output matrix 16 | for(int i=0; i 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | // Matrix Searching in Sorted Matrix 6 | int n , m; 7 | cin>>n>>m; 8 | bool flag = false; 9 | 10 | int mat1[n][m]; 11 | for(int i=0; i>mat1[i][j]; 14 | } 15 | } 16 | // Search for 17 | int num; 18 | cin>>num; 19 | // select {r , c} point 20 | int r=0 , c=m-1; 21 | while (c >= 0 && r num){ 28 | c = c-1; 29 | } 30 | else if (mat1[r][c] < num){ 31 | r = r+1; 32 | } 33 | } 34 | if (flag == false){ 35 | cout<<"Element Not Found"; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /C7JumpsInLoops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int money = 3000; 8 | for(int date=1; date<30; date++ ){ 9 | if (date%2==0){ 10 | continue; 11 | } 12 | if (money==0){ 13 | break; 14 | } 15 | money-=300; 16 | cout<<"Go ware "; 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /C80CharArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | /* 6 | Char Array 7 | - last character is null that describe th eending of an character array in c++ 8 | null describe by '\0' 9 | 10 | */ 11 | char arr1[100] = "apple"; 12 | int i = 0; 13 | 14 | while (arr1[i] != '\0'){ 15 | cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | /* 6 | Character is always is like an array 7 | */ 8 | char arr1[100]; // It only store 100 character string or array 9 | cin>>arr1; // Input Simple Variable 10 | 11 | // Output 12 | cout< 2 | using namespace std; 3 | 4 | int32_t main(){ 5 | /* 6 | Check Palindrome 7 | */ 8 | 9 | int n; // total letter in word 10 | cin>>n; 11 | // Create an char array 12 | char arr1[n+1]; 13 | 14 | // take input of World 15 | cin>>arr1; 16 | 17 | bool Check = 1; 18 | 19 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | /* 6 | Find Larget Word in an Sentence 7 | */ 8 | int n; 9 | cin>>n; 10 | cin.ignore(); // clear the input buffer 11 | 12 | char arr1[n+1]; 13 | // If we want to input all line we use 14 | // cin.getline(word ,n) : // it ignore the white spaces 15 | 16 | cin.getline(arr1 , n+1); 17 | cin.ignore(); 18 | 19 | // cout< (MaxEleE - MaxEleS)){ 32 | MaxEleE = currEleE-1; 33 | MaxEleS = currEleS; 34 | } 35 | currEleS = i+1; 36 | currEleE = i+1; 37 | } 38 | else if((i == n-1) && ((MaxEleE - MaxEleS)<(n-1 - currEleS))){ 39 | // cout<<"yes"< 4 | using namespace std; 5 | 6 | int main(){ 7 | int a = 100; // Normal variable 8 | 9 | // Intialize pointer : pointer is on hexadecial form 10 | int *aptr; // int pointer : datatype of pointer depend upon variable datatype 11 | aptr = &a; // store address of a in pointer 12 | 13 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[] = {10,20,30,40,50}; 6 | // cout<<*arr< 2 | using namespace std; 3 | 4 | int main(){ 5 | int var1 = 10; 6 | 7 | int *pt1 = &var1; 8 | int **pt2 = &pt1; 9 | 10 | cout< 2 | using namespace std; 3 | 4 | // Call by refeerence concept 5 | 6 | void increment (int *a){ 7 | ++*a; 8 | } 9 | 10 | void swap2(int *a , int *b){ 11 | int temp = *a; 12 | *a = *b; 13 | *b = temp; 14 | } 15 | 16 | int main(){ 17 | 18 | int a = 1; 19 | increment(&a); 20 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | // Heap based on dynamic allocation memory 6 | 7 | int a = 15; //Stored in Stack 8 | int *p = new int(); // allocate memory in heap 9 | *p = 10; 10 | cout<<*p< 2 | using namespace std; 3 | 4 | int main(){ 5 | // Allocation 6 | int *p = new int[4]; 7 | p[0] = 1; 8 | p[1] = 2; 9 | p[2] = 3; 10 | p[3] = 4; 11 | 12 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int start , end; 8 | cin>>start>>end; 9 | int i , y; 10 | for (i=start; i<=end; i++){ 11 | for (y=2; y<=i-1; y++){ 12 | if(i%y == 0){ 13 | // non prime 14 | break; 15 | } 16 | } 17 | if(i == y){ 18 | cout<<"Prime : "< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | // String Decimal 7 | string str1; 8 | 9 | // Input of an String Dec 10 | cin>>str1; 11 | // Output of an String Dec 12 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string str1; 7 | 8 | getline(cin, str1); 9 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Append a String to Another string 8 | string str1 = "Ved"; 9 | string str2 = "Gupta"; 10 | 11 | // ---------- create new string with s1 and s2 12 | string str3 = str1 + str2; 13 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string str1 = "abcd"; 7 | string str2 = "abcd"; 8 | 9 | // Return 0 if strings are equal 10 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string str1 = "My name is Ved Gupta"; 7 | 8 | // del ved ffrom string by usimng erase mrthod 9 | // syntax : string.erase(startIndex , howMuchCharFromStart) 10 | str1.erase(11 , 4); 11 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string intro = "Hello my name is Ved Gupta"; 7 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string intro = "My Name is Ved Gupta"; 7 | 8 | // create Ved Gupta substring from intro 9 | string name = intro.substr(11 , intro.length()-1); 10 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string s1 = "78956"; 7 | int n1 = 12345; 8 | 9 | // String to Int 10 | int s2 = stoi(s1); 11 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | // sort an String 9 | string alp = "yuioopla"; 10 | sort(alp.begin(), alp.end()); 11 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | string str1 = "agdhdhTskjvbkjc"; // lower case string 9 | // lower case 32 char greater than lower case 10 | 11 | for (int i=0; i='a' && str1[i]<='z'){ 14 | str1[i] -= 32; 15 | } 16 | } 17 | cout<= 'A' && str2[i] <= 'Z'){ 24 | str2[i] += 32; 25 | } 26 | } 27 | cout< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | // Switch case 8 | cout<<"__Calculator with Switch CAse__\n"; 9 | int a ; 10 | cin>>a; 11 | 12 | switch(a){ 13 | float b , c; 14 | case 1: 15 | 16 | cout<<"Addition\n"; 17 | cin>>b>>c; 18 | cout<<"Sum is "<>b>>c; 23 | cout<<"Sum is "<>b>>c; 28 | cout<<"Sum is "<>b>>c; 33 | cout<<"Sum is "< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | vector v; 9 | 10 | v.push_back(1); 11 | v.push_back(3); 12 | v.push_back(6); 13 | v.push_back(7); 14 | 15 | cout<<"Check 5 is present or Not --> "< "< "< "<"<"<"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | // Initialize an Array 8 | array a = {1,2,3,4}; 9 | 10 | cout<<"Check Array Size : "< 2 | #include 3 | 4 | using namespace std; 5 | int main(){ 6 | 7 | // Initialize vector 8 | vector v; 9 | /* 10 | vector v1(5 , 1); //size 5 and initialize by 1 11 | // copy one vector to another 12 | vector newvector(v1) 13 | */ 14 | cout<<"Capacity-->"<"<"<"<"<"<"<"<"<"<"< 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | // Dynamic in Nature 10 | deque d; 11 | 12 | // push back 13 | d.push_back(1); 14 | d.push_back(3); 15 | 16 | // push front 17 | d.push_front(2); 18 | d.push_front(4); 19 | 20 | for(int i:d){ 21 | cout<"<"<"<"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | // Not randomial acces 8 | list l; 9 | 10 | // copy: list l2(l) 11 | // intialize by element (size , element) 12 | // list l3(5,100); 13 | 14 | l.push_back(1); 15 | l.push_front(2); 16 | 17 | for(int ele:l){ 18 | cout<"< l3(5,100); 33 | list l3(5,100); 34 | for(int ele:l3){ 35 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | // Initialize an string type Stack 7 | stack s; 8 | s.push("Ved"); 9 | s.push("Prakash"); 10 | s.push("Gupta"); 11 | 12 | cout<<"Top-Element-->"<"<"< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | // initialize queue of strring stype 7 | queue q1; 8 | 9 | q1.push("Ved"); 10 | q1.push("prakash"); 11 | q1.push("Gupta"); 12 | 13 | cout<<"Size-->"< "<"< "< 2 | #include 3 | 4 | // First element is all greatest 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | priority_queue p_max; //max heap like 10 | priority_queue ,greater> p_min; // mini heap like 11 | 12 | // push on max 13 | p_max.push(2); 14 | p_max.push(3); 15 | p_max.push(5); 16 | p_max.push(4); 17 | p_max.push(0); 18 | 19 | cout<<"Transeverse p_max : "; 20 | 21 | int n = p_max.size(); //always initialize befor 22 | for(int i=0; i "< 2 | #include 3 | 4 | using namespace std; 5 | int main(){ 6 | 7 | set s; 8 | 9 | s.insert(5); 10 | s.insert(4); 11 | s.insert(4); 12 | s.insert(4); 13 | s.insert(2); 14 | s.insert(2); 15 | s.insert(1); 16 | s.insert(1); 17 | s.insert(1); 18 | s.insert(6); 19 | s.insert(6); 20 | s.insert(6); 21 | 22 | for (auto i:s){ 23 | // sorted set prind i order 24 | cout<::iterator it = s.begin(); 30 | it++; 31 | s.erase(it); 32 | 33 | for (auto i:s){ 34 | cout< "< "<::iterator it5 = s.find(5); 44 | for(auto it=it5; it!=s.end(); it++){ 45 | cout<<*it<<" "; 46 | } 47 | cout< 2 | #include 3 | 4 | // O(n) time Complexity 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | map m; 10 | m[1] = "Ved"; 11 | m[2] = "Prakash"; 12 | m[5] = "Gupta"; 13 | 14 | m.insert({10 , "My Nsme"}); 15 | 16 | // Before Erase Map 17 | for (auto i: m){ 18 | cout< "< 2 | using namespace std; 3 | int main() 4 | { 5 | int t1, t2; 6 | int n; 7 | cin >> t1; 8 | cin >> t2; 9 | cin >> n; 10 | 11 | vector arr1; 12 | for (int i = 0; i < n; i++) 13 | { 14 | int temp; 15 | cin >> temp; 16 | arr1.push_back(temp); 17 | } 18 | sort(arr1.begin(), arr1.end()); 19 | vector f; 20 | vector l; 21 | int c1 = 0; 22 | int c2 = 0; 23 | int j = t1 * arr1[n - 1]; 24 | int k = 0; 25 | f.push_back(arr1[n - 1]); 26 | c1++; 27 | for (int i = n - 2; i >= 0; i--) 28 | { 29 | int first = j + (t1 * arr1[i]); 30 | int second = k + (t2 * arr1[i]); 31 | 32 | if (first < second) 33 | { 34 | f.push_back(arr1[i]); 35 | c1++; 36 | second = second - (t2 * arr1[i]); 37 | if (first < second) 38 | { 39 | j = 0; 40 | k = second - first; 41 | } 42 | else 43 | { 44 | k = 0; 45 | j = first - second; 46 | } 47 | } 48 | else 49 | { 50 | first = first - (t1 * arr1[i]); 51 | l.push_back(arr1[i]); 52 | c2++; 53 | if (first < second) 54 | { 55 | j = 0; 56 | k = second - first; 57 | } 58 | else 59 | { 60 | k = 0; 61 | j = first - second; 62 | } 63 | } 64 | } 65 | 66 | for (int i = 0; i < c1; i++) 67 | { 68 | if (i == c1 - 1) 69 | { 70 | cout << f[i]; 71 | } 72 | else 73 | { 74 | cout << f[i] << " "; 75 | } 76 | } 77 | cout << endl; 78 | 79 | for (int i = 0; i < c2; i++) 80 | { 81 | if (i == c2 - 1) 82 | { 83 | cout << l[i]; 84 | } 85 | else 86 | { 87 | cout << l[i] << " "; 88 | } 89 | } 90 | cout << endl; 91 | } -------------------------------------------------------------------------------- /LinkedList/L0Node.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public : 6 | int data; 7 | Node * next; 8 | 9 | Node(int data){ 10 | this->data = data; 11 | this->next = NULL; 12 | } 13 | }; 14 | 15 | int main(){ 16 | 17 | // create Node 18 | Node* node1 = new Node(10); 19 | 20 | //print node 21 | cout<< node1 -> data < next < 2 | using namespace std; 3 | 4 | class Node 5 | { 6 | public: 7 | int data; 8 | Node *next; 9 | 10 | Node(int data) 11 | { 12 | this->data = data; 13 | this->next = NULL; 14 | } 15 | 16 | ~Node() 17 | { 18 | int value = this->data; 19 | if (this->next != NULL) 20 | { 21 | delete next; 22 | this->next = NULL; 23 | } 24 | cout << "Memory Free Node with Data : " << value << endl; 25 | } 26 | }; 27 | 28 | void InsertAtHead(Node *&head, int data) 29 | { 30 | Node *node1 = new Node(data); 31 | node1->next = head; 32 | head = node1; 33 | } 34 | 35 | void InsertAtTail(Node *&tail, int data) 36 | { 37 | Node *temp = new Node(data); 38 | tail->next = temp; 39 | tail = temp; 40 | } 41 | 42 | void InsertAtPosition(Node *&head, Node *&tail, int data, int pos) 43 | { 44 | if (pos == 1) 45 | { 46 | InsertAtHead(head, data); 47 | return; 48 | } 49 | 50 | Node *temp = head; 51 | int cnt = 1; 52 | while (cnt != pos) 53 | { 54 | temp = temp->next; 55 | if (temp->next == NULL) 56 | { 57 | InsertAtTail(tail, data); 58 | return; 59 | } 60 | cnt++; 61 | } 62 | Node *node1 = new Node(data); 63 | node1->next = temp->next; 64 | temp->next = node1; 65 | } 66 | 67 | void DeleteAtPosition(Node *&head, Node* &tail , int pos) 68 | { 69 | if (pos == 1) 70 | { 71 | Node *temp = head; 72 | head = head->next; 73 | temp->next = NULL; 74 | delete temp; 75 | } 76 | else 77 | { 78 | Node *curr = head; 79 | Node *prev = NULL; 80 | int cnt = 1; 81 | while (cnt <= pos) 82 | { 83 | prev = curr; 84 | curr = curr->next; 85 | if(curr->next == NULL){ 86 | tail = prev; 87 | } 88 | cnt++; 89 | } 90 | prev->next = curr -> next; 91 | curr->next = NULL; 92 | delete curr; 93 | } 94 | } 95 | 96 | void Print(Node *&head) 97 | { 98 | Node *temp = head; 99 | while (temp->next != NULL) 100 | { 101 | cout << temp->data << " "; 102 | temp = temp->next; 103 | } 104 | cout << temp->data << endl; 105 | } 106 | 107 | int main() 108 | { 109 | 110 | // create Nod 111 | Node *start = new Node(12); 112 | 113 | Node *head = start; 114 | Node *tail = start; 115 | 116 | // Insert At Head 117 | InsertAtHead(head, 29); 118 | InsertAtHead(head, 3); 119 | InsertAtHead(head, 1); 120 | InsertAtHead(head, 9); 121 | 122 | // Print Linked List 123 | Print(head); 124 | 125 | // Insert At Tail 126 | InsertAtTail(tail, 0); 127 | InsertAtTail(tail, 23); 128 | 129 | // Print Linked List 130 | Print(head); 131 | 132 | // Insert At position 133 | InsertAtPosition(head, tail, 5, 3); 134 | InsertAtPosition(head, tail, 55, 1); 135 | InsertAtPosition(head, tail, 999, 50); 136 | 137 | // Print Linked List 138 | Print(head); 139 | 140 | // Insert At Tail 141 | InsertAtTail(tail, 500); 142 | 143 | // Print Linked List 144 | Print(head); 145 | 146 | // Verify Head and Tail 147 | cout << "Head->" << head->data << endl; 148 | cout << "Tail->" << tail->data << endl; 149 | 150 | // Delete at position 151 | DeleteAtPosition(head, tail, 1); 152 | Print(head); 153 | 154 | DeleteAtPosition(head, tail, 4); 155 | Print(head); 156 | 157 | DeleteAtPosition(head, tail, 8); 158 | Print(head); 159 | 160 | DeleteAtPosition(head, tail, 6); 161 | Print(head); 162 | 163 | InsertAtTail(tail , 600); 164 | Print(head); 165 | 166 | return 0; 167 | } -------------------------------------------------------------------------------- /LinkedList/L2DoublyLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node* prev; 8 | Node* next; 9 | 10 | Node(int d){ 11 | this->data = d; 12 | this->prev = NULL; 13 | this->next = NULL; 14 | } 15 | }; 16 | 17 | // Taversing 18 | void print(Node* head){ 19 | Node* temp = head; 20 | while(temp!=NULL){ 21 | cout<data<<" "; 22 | temp = temp->next; 23 | } 24 | cout<next; 34 | } 35 | return cnt; 36 | } 37 | 38 | void InsertAtHead(Node* &head , int data){ 39 | Node *temp = new Node(data); 40 | head->prev = temp; 41 | temp->next = head; 42 | head = temp; 43 | } 44 | 45 | int main(){ 46 | Node* node1 = new Node(10); 47 | Node* head = node1; 48 | 49 | InsertAtHead(head , 22); 50 | InsertAtHead(head , 11); 51 | 52 | print(head); 53 | 54 | // Get Length 55 | cout< 2 | using namespace std; 3 | 4 | class Animal{ 5 | public: 6 | void eat(){ 7 | cout<<"Eating...."< 2 | using namespace std; 3 | 4 | class Animal { 5 | //parent class 6 | public: 7 | void eat() { 8 | cout << "eating" << endl; 9 | } 10 | }; 11 | 12 | // Child class 13 | class Dog: public Animal { 14 | public: void bark() { 15 | cout << "barking" << endl; 16 | } 17 | }; 18 | 19 | class BabyDog: public Dog { 20 | // grand chile 2nd level 21 | public: void weep() { 22 | cout << "weeping"; 23 | } 24 | }; 25 | 26 | int main(){ 27 | BabyDog b1; 28 | 29 | b1.eat(); 30 | b1.bark(); 31 | b1.weep(); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /OOPS/O12MultipleInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Parent class 4 | class Animal { 5 | public: 6 | void eat() { 7 | cout << "eating" << endl; 8 | } 9 | }; 10 | // Parent class 11 | class Dog { 12 | public: void bark() { 13 | cout << "barking" << endl; 14 | } 15 | }; 16 | // Inherited class from animal and Dog 17 | class BabyDog: public Animal, public Dog { 18 | public: void weep() { 19 | cout << "weeping"; 20 | } 21 | }; 22 | 23 | 24 | int main() { 25 | // Creating an object of the child class 26 | BabyDog obj; 27 | // calling methods 28 | obj.eat(); 29 | obj.bark(); 30 | obj.weep(); 31 | } -------------------------------------------------------------------------------- /OOPS/O13HeierchicalInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Parent class 4 | class Animal { 5 | public: 6 | void eat() { 7 | cout << "eating" << endl; 8 | } 9 | }; 10 | // child class1 11 | class Dog: public Animal { 12 | public: void bark() { 13 | cout << "barking" << endl; 14 | } 15 | }; 16 | // child class2 17 | class Cat: public Animal { 18 | public: void meow() { 19 | cout << "meowing" << endl; 20 | } 21 | }; 22 | int main() { 23 | Cat obj; 24 | // calling methods 25 | obj.eat(); 26 | obj.meow(); 27 | } -------------------------------------------------------------------------------- /OOPS/O14HybridInheritance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // Parent class1 4 | class Vehicle { 5 | public: 6 | Vehicle() { 7 | cout << "This is a Vehicle" << endl; 8 | } 9 | }; 10 | //Parent class2 11 | class Fare { 12 | public: 13 | Fare() { 14 | cout << "Fare of Vehicle\n"; 15 | } 16 | }; 17 | //Child class1 18 | class Car: public Vehicle { 19 | }; 20 | //Child class2 21 | class Bus: public Vehicle, public Fare { 22 | }; 23 | // main function 24 | int main() { 25 | // creating object of sub class will 26 | // invoke the constructor of base class 27 | Bus obj2; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /OOPS/O15MultipleInheritanceAmbiguity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A { 5 | public: 6 | void abc() { 7 | cout << "Class A"< 2 | using namespace std; 3 | 4 | /* 5 | Static Polmorphism or compile time polymorphism 6 | - function Overloading 7 | : only parameter change not return type 8 | - Operator Overloading 9 | + operator some specific work 10 | 11 | */ 12 | 13 | /* 14 | operators that can be operloaded 15 | 16 | */ 17 | 18 | class A{ 19 | public : 20 | void sayHello(){ 21 | cout<<"Hello Ved Gupta"<num; 34 | int arg = obj.num; 35 | cout<num; 40 | int arg = obj.num; 41 | cout< 10 | 11 | += 12 | 13 | -= 14 | 15 | *= 16 | 17 | ⁄= 18 | 19 | %= 20 | 21 | ‸= 22 | 23 | &= 24 | 25 | |= 26 | 27 | << 28 | 29 | >> 30 | 31 | <<= 32 | 33 | >>= 34 | 35 | == 36 | 37 | != 38 | 39 | <= 40 | 41 | >= 42 | 43 | && 44 | 45 | || 46 | 47 | ++ 48 | 49 | — 50 | 51 | , 52 | 53 | ->* 54 | 55 | -> 56 | 57 | ( ) 58 | 59 | [ ] 60 | 61 | new 62 | 63 | delete 64 | 65 | new[] 66 | 67 | delete[] 68 | 69 | 70 | 71 | ##operators that cannot be operloaded in c++ 72 | 73 | :: 74 | 75 | .* 76 | 77 | . 78 | 79 | ?: -------------------------------------------------------------------------------- /OOPS/O17RuntimePolymorphismEx.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Runtime Polymorphism : only possible in multilevel Inheritance 6 | - Function / Method Overriding 7 | */ 8 | class Animal{ 9 | public: 10 | void speak(){ 11 | cout<<"Speak..."< 2 | using namespace std; 3 | 4 | class Hero{ 5 | // blank class iss of 1 Byte 6 | }; 7 | 8 | int main(){ 9 | Hero h1; 10 | cout<<"Size of Hero h1 --> "< 9 | using namespace std; 10 | class abstraction { 11 | private: 12 | int a, b; 13 | public: 14 | // method to set values of private members 15 | void set(int x, int y) { 16 | a = x; 17 | b = y; 18 | } 19 | void display() { 20 | cout << "a = " << a << endl; 21 | cout << "b = " << b << endl; 22 | } 23 | }; 24 | int main() { 25 | abstraction obj; 26 | obj.set(10, 20); 27 | obj.display(); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /OOPS/O2AccesDataMembersInClass.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Hero{ 5 | // roperties by default private 6 | int health; 7 | 8 | // public 9 | public : 10 | char level; 11 | }; 12 | 13 | int main(){ 14 | Hero h1; 15 | // cout<<"Hero Health --> " < "< 2 | using namespace std; 3 | 4 | class Student{ 5 | int id; 6 | 7 | public : 8 | string name ; 9 | int num ; 10 | 11 | void setId(int id1){ 12 | id = id1; 13 | } 14 | int getId(){ 15 | return id; 16 | } 17 | void me(){ 18 | cout<<"Hey i am "<"<"< 2 | using namespace std; 3 | 4 | class Student{ 5 | private: 6 | int id; 7 | public : 8 | string name ; 9 | 10 | void setId(int id1){ 11 | id = id1; 12 | } 13 | int getId(){ 14 | return id; 15 | } 16 | void me(){ 17 | cout<<"Hey i am "<"<"<setId(545630); 37 | S2->me(); 38 | cout<<"Size-->"< 2 | using namespace std; 3 | 4 | class Student{ 5 | private : 6 | int id; 7 | 8 | public: 9 | string name; 10 | Student(){ 11 | // automatic invoked 12 | cout<<"Unparameterized Created"<id = id; 18 | this->name = name; 19 | } 20 | }; 21 | 22 | int main(){ 23 | Student S1; 24 | Student S2(1245 , "Ved Gupta"); 25 | 26 | cout<<"S1 : Name->"<"< S3 : Name->"< 2 | using namespace std; 3 | 4 | class Student{ 5 | public : 6 | int id; 7 | string name; 8 | 9 | Student(int n){; 10 | cout<<"Constructor Called"< 2 | using namespace std; 3 | 4 | class Student{ 5 | public: 6 | int id; 7 | string name; 8 | static int year; 9 | 10 | Student(int id , string name){ 11 | this->id = id; 12 | this->name = name; 13 | } 14 | //static function only acces static member 15 | static void getyear(){ 16 | cout<<"year->"<"<"< 2 | using namespace std; 3 | 4 | // Encapsulate data members and behavior : functions/methods iin single entity Class known as Encpdulation 5 | 6 | // A fully enccapsulated class is classs where all data members is private 7 | class person{ 8 | private: 9 | string name; 10 | int age; 11 | 12 | public: 13 | person(string name , int age){ 14 | this->name = name; 15 | this->age = age; 16 | } 17 | int getAge(){ 18 | return this->age; 19 | } 20 | 21 | string getName(){ 22 | return this->name; 23 | } 24 | }; 25 | 26 | int main(){ 27 | person p1("Ved Gupta" , 20); 28 | cout<<"Name->"<"< 2 | using namespace std; 3 | 4 | class person{ 5 | private: 6 | string name; 7 | int age; 8 | 9 | public: 10 | void setAge(int age){ 11 | this->age = age; 12 | } 13 | void setName(string name){ 14 | this->name = name; 15 | } 16 | int getAge(){ 17 | return this->age; 18 | } 19 | 20 | string getName(){ 21 | return this->name; 22 | } 23 | }; 24 | 25 | class Male : public person{ 26 | private: 27 | char gender = 'm'; 28 | public: 29 | char getGender(){ 30 | return this->gender; 31 | } 32 | }; 33 | 34 | int main(){ 35 | Male m ; 36 | cout<<"Gender --> "< 2 | using namespace std; 3 | 4 | void printArray(int arr[] , int s , int e){ 5 | cout<<"Size-->"<e){ 15 | return 0; 16 | } 17 | int mid = s + (e-s)/2; 18 | 19 | printArray(arr , s , e); 20 | 21 | if(arr[mid] == ele){ 22 | return 1; 23 | } 24 | if(arr[mid] > ele){ 25 | return BinarySearch(arr , s , mid-1 , ele); 26 | } 27 | if (arr[mid] < ele){ 28 | return BinarySearch(arr , mid+1 , e , ele); 29 | } 30 | return 0; 31 | } 32 | 33 | int main(){ 34 | 35 | int arr[7] = {2,5,6,7,9,15,21}; 36 | int ele = 6; 37 | 38 | int res = BinarySearch(arr , 0 , 6 , ele); 39 | if(res){ 40 | cout<<"Element found "< 2 | using namespace std; 3 | 4 | int firstOccurence(int arr[] , int s , int e , int ele){ 5 | if(s>e){ 6 | return -1; 7 | } 8 | int mid = s + (e-s)/2; 9 | 10 | if(arr[mid] == ele){ 11 | int flag = mid; 12 | int checkfirst = firstOccurence(arr , s , mid-1 , ele); 13 | if (checkfirst == -1){ 14 | return flag; 15 | } 16 | else{ 17 | return checkfirst; 18 | } 19 | } 20 | 21 | if(arr[mid]>ele){ 22 | return firstOccurence(arr , s , mid-1 , ele); 23 | } 24 | if(arr[mid]e){ 32 | return -1; 33 | } 34 | int mid = s + (e-s)/2; 35 | 36 | if(arr[mid] == ele){ 37 | int flag = mid; 38 | int checklast = lastOccurence(arr , mid+1 , e , ele); 39 | if (checklast == -1){ 40 | return flag; 41 | } 42 | else{ 43 | return checklast; 44 | } 45 | } 46 | 47 | if(arr[mid]>ele){ 48 | return lastOccurence(arr , s , mid-1 , ele); 49 | } 50 | if(arr[mid] "< "< 2 | using namespace std; 3 | 4 | int firstOccurence(int arr[] , int s , int e , int ele){ 5 | if(s>e){ 6 | return -1; 7 | } 8 | int mid = s + (e-s)/2; 9 | 10 | if(arr[mid] == ele){ 11 | int flag = mid; 12 | int checkfirst = firstOccurence(arr , s , mid-1 , ele); 13 | if (checkfirst == -1){ 14 | return flag; 15 | } 16 | else{ 17 | return checkfirst; 18 | } 19 | } 20 | 21 | if(arr[mid]>ele){ 22 | return firstOccurence(arr , s , mid-1 , ele); 23 | } 24 | if(arr[mid]e){ 32 | return -1; 33 | } 34 | int mid = s + (e-s)/2; 35 | 36 | if(arr[mid] == ele){ 37 | int flag = mid; 38 | int checklast = lastOccurence(arr , mid+1 , e , ele); 39 | if (checklast == -1){ 40 | return flag; 41 | } 42 | else{ 43 | return checklast; 44 | } 45 | } 46 | 47 | if(arr[mid]>ele){ 48 | return lastOccurence(arr , s , mid-1 , ele); 49 | } 50 | if(arr[mid] "< 2 | using namespace std; 3 | 4 | int findPeakInMountainArray(int arr[] , int s , int e){ 5 | if (s>e){ 6 | return -1; 7 | } 8 | int mid = s + (e-s)/2; 9 | if(arr[mid] > arr[mid+1] && arr[mid]>arr[mid-1]){ 10 | return mid; 11 | } 12 | if(arr[mid] < arr[mid+1]){ 13 | return findPeakInMountainArray(arr , mid , e); 14 | } 15 | if(arr[mid] > arr[mid+1] && arr[mid]"< 2 | using namespace std; 3 | 4 | bool checkPalindrome(string name , int s , int e ){ 5 | if(s>=e){ 6 | return 1; 7 | } 8 | if (name[s] == name[e]){ 9 | return checkPalindrome(name , s+1 , e-1); 10 | } 11 | else{ 12 | return 0; 13 | } 14 | return 0; 15 | } 16 | 17 | int main(){ 18 | string name = "abcddcba"; 19 | int size = name.length(); 20 | 21 | cout<<"isPalindrome--> "< 2 | using namespace std; 3 | 4 | int power(int base , int pow){ 5 | if (pow == 0){ 6 | return 1; 7 | } 8 | if (pow == 1){ 9 | return base; 10 | } 11 | 12 | int ans = power(base , pow/2); 13 | if (pow%2==0){ 14 | return ans*ans; 15 | } 16 | else{ 17 | return base*ans*ans; 18 | } 19 | } 20 | 21 | int main(){ 22 | int base = 2; 23 | int pow = 7; 24 | 25 | cout<"< 2 | 3 | using namespace std; 4 | 5 | void PrintArray(int arr[] , int n){ 6 | for (int i=0; i arr[i+1]){ 18 | swap(arr[i] , arr[i+1]); 19 | } 20 | } 21 | BubbleSort(arr , n-1); 22 | } 23 | 24 | int main(){ 25 | int arr[8] = {7,8,1,2,4,77,5,2}; 26 | int n = 8; 27 | 28 | cout<<"Before-> "; 29 | PrintArray(arr , n); 30 | cout< "; 35 | PrintArray(arr , n); 36 | cout< 2 | using namespace std; 3 | 4 | void reverseString(string &name , int s , int e ){ 5 | if(s>e){ 6 | return ; 7 | } 8 | swap(name[s] , name[e]); 9 | reverseString(name , s+1 , e-1); 10 | } 11 | 12 | int main(){ 13 | string name = "Ved Prakash Gupta"; 14 | int size = name.length(); 15 | reverseString(name , 0 , size-1); 16 | cout< 2 | using namespace std; 3 | 4 | void PrintArray(int arr[] , int s , int e){ 5 | for (int i=s; i<=e; i++){ 6 | cout< "; 14 | PrintArray(arr , start , end); 15 | cout< "; 57 | PrintArray(arr , start , end); 58 | cout<= end){ 67 | return; 68 | } 69 | int mid = start + (end - start)/2; 70 | 71 | MergeSort(arr , start , mid); 72 | MergeSort(arr , mid+1 , end); 73 | 74 | merge(arr , start , end); 75 | 76 | } 77 | 78 | int main(){ 79 | int arr[8] = {7,8,91,2,1,4,5,0}; 80 | int n = 8; 81 | 82 | cout<<"Before-> "; 83 | PrintArray(arr , 0 , n-1); 84 | cout< "; 89 | PrintArray(arr , 0 , n-1); 90 | cout< 2 | using namespace std; 3 | 4 | void PrintArray(int arr[] , int s , int e){ 5 | for (int i=s; i<=e; i++){ 6 | cout< arr[i]){ 21 | pivotIndex += 1; 22 | } 23 | } 24 | 25 | // set pivot ele to pivot index 26 | swap(arr[s] , arr[pivotIndex]); 27 | 28 | // pivot element is setteled on correct position 29 | 30 | // sort both side of pivot 31 | int i = s; 32 | int j = e; 33 | 34 | 35 | while(i<=j){ 36 | if(arr[i] > pivot && arr[j] < pivot){ 37 | swap(arr[i] , arr[j]); 38 | i++; 39 | j--; 40 | } 41 | else if(arr[i] < pivot && arr[j] > pivot){ 42 | i++; 43 | j--; 44 | } 45 | else if(arr[i] < pivot && arr[j] < pivot){ 46 | i++; 47 | } 48 | else if(arr[i] > pivot && arr[j] > pivot){ 49 | j--; 50 | } 51 | else{ 52 | break; 53 | } 54 | } 55 | 56 | // cout<<"Steps-> "; 57 | // PrintArray(arr , s , e); 58 | // cout<= e){ 66 | return ; 67 | } 68 | int p = partition(arr , s , e); 69 | 70 | quickSort(arr , s , p); 71 | quickSort(arr , p+1 , e); 72 | } 73 | 74 | int main(){ 75 | int arr[8] = {2,5,1,3,9,7,6,0}; 76 | int size = 8; 77 | 78 | cout<<"Before-> "; 79 | PrintArray(arr , 0 , size-1); 80 | cout< "; 86 | PrintArray(arr , 0 , size-1); 87 | cout< 2 | using namespace std; 3 | 4 | void PrintVector(vector arr){ 5 | cout<<" { "; 6 | for (int i=0; i nums ,vectoroutput , int index){ 13 | if (index >= nums.size()){ 14 | PrintVector(output); 15 | return; 16 | } 17 | 18 | // exclude 19 | PowerSet(nums , output , index+1); 20 | 21 | // include index element 22 | int ele = nums[index]; 23 | output.push_back(ele); 24 | PowerSet(nums , output , index+1); 25 | } 26 | 27 | int main(){ 28 | // find powerset : set of all Subset 29 | vector nums = {1 , 2 , 3}; 30 | vectoroutput; 31 | int index = 0; 32 | 33 | PowerSet(nums , output , index); 34 | 35 | return 0; 36 | 37 | 38 | } -------------------------------------------------------------------------------- /Recursion/R1findpower2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int powerFind(int base , int n){ 6 | if (n==0){ 7 | return 1; 8 | } 9 | return base*powerFind(base , n-1); 10 | } 11 | int main(){ 12 | 13 | cout< 2 | using namespace std; 3 | 4 | 5 | void SubSequences(string in_str ,string output , int index){ 6 | if (index >= in_str.length()){ 7 | cout< 2 | using namespace std; 3 | 4 | string keypad[10] = {"" , "" , "abc" , "def" , "ghi" , "jkl" , "mno" , "pqrs" , "tuv" , "wxyz"}; 5 | 6 | void Solution(string nums , string out , int index){ 7 | if (index >= nums.length()){ 8 | cout< 2 | using namespace std; 3 | 4 | void permutation(string str , int index){ 5 | if(index >= str.length()){ 6 | cout< 2 | using namespace std; 3 | 4 | 5 | bool isSafe(int x , int y , int n, vector> &arr, vector> &visitedArr){ 6 | 7 | if((x >= 0 && x < n) && (y>=0 && y> &arr , int n , int x , int y , vector> &visitedArr , string path){ 21 | if(x == n-1 && y == n-1){ 22 | cout<> arr = { 71 | {1,0,0,0}, 72 | {1,1,0,1}, 73 | {1,1,0,0}, 74 | {0,1,1,1} 75 | }; 76 | 77 | vector> visitedArr = arr; // initialize by 0 78 | int n = 4; 79 | for(int i = 0; i 2 | using namespace std; 3 | 4 | int factorial(int n){ 5 | if (n==1 || n==0){ 6 | return 1; 7 | } 8 | return n*factorial(n-1); 9 | } 10 | 11 | int main(){ 12 | 13 | cout< 2 | 3 | using namespace std; 4 | 5 | void print(int n){ 6 | if (n==0){ 7 | return; 8 | } 9 | print(n-1); 10 | cout< 2 | 3 | using namespace std; 4 | 5 | int fibonachi(int n){ 6 | if (n<1){ 7 | return 0; 8 | } 9 | if (n==1){ 10 | return 1; 11 | } 12 | return fibonachi(n-1) + fibonachi(n-2); 13 | } 14 | 15 | int main(){ 16 | // 0 1 1 2 3 5 8 13 21 34 17 | cout << fibonachi(5) < 2 | 3 | using namespace std; 4 | 5 | int StepLadder(int n){ 6 | if(n==0){ 7 | return 1; 8 | } 9 | else if(n<0){ 10 | return 0; 11 | } 12 | return StepLadder(n-1) + StepLadder(n-2); 13 | } 14 | 15 | int main(){ 16 | // 1 or 2 step 17 | cout< 2 | 3 | using namespace std; 4 | 5 | void sayDigit(int n){ 6 | string numbers[10] = {"zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine"}; 7 | if (n==0){ 8 | return; 9 | } 10 | int temp = n%10; 11 | sayDigit(n/10); 12 | cout<< numbers[temp]<<" "; 13 | } 14 | 15 | int main(){ 16 | int n; 17 | cin>>n; 18 | sayDigit(n); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Recursion/R7CheckArrayIsSortedRecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | bool isSorted(int arr[] , int size){ 6 | if (size == 0 && size == 1){ 7 | return 1; 8 | } 9 | if (arr[0] > arr[1]){ 10 | return 0; 11 | } 12 | return isSorted(arr+1 , size-1); 13 | 14 | } 15 | 16 | 17 | int main(){ 18 | int arr[5] = {2,4,7,9,13}; 19 | int size = 5; 20 | 21 | bool ans = isSorted(arr , size); 22 | if(ans){ 23 | cout<<"Array is Sorted"< 2 | 3 | using namespace std; 4 | 5 | int ArraySum(int arr[] , int size){ 6 | if (size == 0 ){ 7 | return 0; 8 | } 9 | if (size == 1){ 10 | return arr[0]; 11 | } 12 | return arr[0] + ArraySum(arr+1 , size-1); 13 | } 14 | 15 | int main(){ 16 | 17 | int arr[5] = {2,4,6,8,9}; 18 | int size = 5; 19 | 20 | cout< 2 | using namespace std; 3 | 4 | void printArray(int arr[] , int size){ 5 | cout<<"Size-->"< 2 | using namespace std; 3 | 4 | void SolveKeyPadProblem(string keypad[] , string nums , int index , string out){ 5 | if(index >= nums.length()){ 6 | cout< 2 | using namespace std; 3 | 4 | int BinarySearch(int arr[] , int size , int key){ 5 | int start = 0; 6 | int end = size - 1; 7 | 8 | // int mid = (start + end) / 2; 9 | int mid = start + (end - start)/2; 10 | 11 | while(start<=end){ 12 | if(arr[mid]==key){ 13 | return mid; 14 | } 15 | else if (arr[mid] key){ 19 | end = mid-1; 20 | } 21 | // mid = (start + end)/2; 22 | mid = start + (end - start)/2; 23 | } 24 | return -1; 25 | } 26 | 27 | int main(){ 28 | /* 29 | [-find Mid] 30 | */ 31 | 32 | int n; 33 | cin>>n; 34 | 35 | int arr1[n]; 36 | 37 | for (int i=0; i>arr1[i]; 39 | } 40 | 41 | int key; 42 | cin>>key; 43 | 44 | cout< 2 | using namespace std; 3 | 4 | int FirstOccurence(int arr[] , int n , int key){ 5 | int start = 0; 6 | int end = n-1; 7 | int mid = start + (end - start)/2; 8 | int res = -1; 9 | while (start <= end){ 10 | if (arr[mid]==key){ 11 | res = mid; 12 | end = mid-1; 13 | } 14 | else if (arr[mid]>key){ 15 | end = mid-1; 16 | } 17 | else if (arr[mid]key){ 36 | end = mid-1; 37 | } 38 | else if (arr[mid]>n; 52 | 53 | int arr[n]; 54 | for(int i=0; i>arr[i]; 56 | } 57 | cin>>k; 58 | 59 | cout< 2 | using namespace std; 3 | 4 | int FirstOccurence(int arr[] , int n , int key){ 5 | int start = 0; 6 | int end = n-1; 7 | int mid = start + (end - start)/2; 8 | int res = -1; 9 | while (start <= end){ 10 | if (arr[mid]==key){ 11 | res = mid; 12 | end = mid-1; 13 | } 14 | else if (arr[mid]>key){ 15 | end = mid-1; 16 | } 17 | else if (arr[mid]key){ 36 | end = mid-1; 37 | } 38 | else if (arr[mid]>n; 52 | 53 | int arr[n]; 54 | for(int i=0; i>arr[i]; 56 | } 57 | cin>>k; 58 | 59 | cout< 2 | using namespace std; 3 | 4 | int FindPivot(int arr[] , int n){ 5 | int start = 0; 6 | int end = n-1; 7 | int mid = start +(end-start)/2; 8 | 9 | while(s<=e){ 10 | if(arr[mid] 2 | using namespace std; 3 | 4 | int FindPeakInMountain(int arr[] , int n){ 5 | int start = 0 , end = n-1; 6 | int mid = start + (end-start)/2; 7 | while(start < end){ 8 | if (arr[mid]>n; 22 | int arr[n]; 23 | for(int i=0; i>arr[i]; 25 | } 26 | 27 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | } --------------------------------------------------------------------------------