├── Array ├── 01 static and dynamic array .c ├── 02 increasing size of array using heap.cpp ├── 03 Creating 2D array & Accessing.c ├── 04 Array ADT.c ├── 05 inserting an element.c ├── 06 Deleting element from array.c ├── 07 Linear search.c ├── 08 Improving Linear search.c ├── 09 Binary Search using loop.c ├── 10 Binary Search Using Recursion.c ├── 11 Get Set max min sum avg.c ├── 12 Rotate and Shift.c ├── 13 IsSorted .c ├── 14 Mergeing 2 Array.cpp ├── 15 InsertSort .c ├── 16 Set Operation (Union).cpp ├── 17 Insertion and Deletion.cpp ├── 18 Menu Based problem.cpp ├── 19 Single Missing Element in Sorted Array.cpp ├── 20 Single Missing Value in a sorted array M2.cpp ├── 21 Multiple Missing Elements in Sorted Array.cpp ├── 22 Missing elements in Unsorted Array (HASH TABLE).cpp ├── 23 Finding Duplicate in Soorted Array.cpp ├── 24 Counting Duplicate Elements In Sorted.cpp ├── 25 Counting Duplicate elements using hash table (SA).cpp ├── 25 b. Find Duplicate in unsorted array using hash table.cpp ├── 26 Finding Duplicate in Unsoorted Array.cpp ├── 27 Find a pair with sum k.cpp ├── 28 Find a pair with sum k in unsorted.cpp ├── 29 Find a pair with sum k using Hashing.cpp ├── 30 Finding max & min in single scan.cpp ├── 31 Menu for Array.c ├── 32 Intersection of two Array.cpp ├── 33 Find a peak element in an array.java ├── Geeksforgeeks problem │ ├── 01 Subarray with given sum.cpp │ ├── 02 Count the triplets.cpp │ ├── 03 Sum of array elements.cpp │ ├── 04 Find Missing And Repeating.cpp │ ├── 05 Merge Without Extra Space.cpp │ └── string reverse.cpp ├── Problems │ ├── 01 Print number in words.cpp │ ├── 02 Search in a matrix.cpp │ ├── 03 Product array puzzle.cpp │ ├── 04 Maximum Index.cpp │ ├── 05 Spirally traversing a matrix.cpp │ ├── 06 Average of Array.cpp │ ├── 07 Sum of Matrix.cpp │ ├── 08 Frequency of number.cpp │ ├── 09 Fencing Ground.cpp │ ├── 10 Find Largest Element in an Array.cpp │ ├── 11 Swapping Adjacent Element.cpp │ ├── 12 Merge array.cpp │ ├── 13 Merge Array & sort.cpp │ ├── 14 Find median in a stream.cpp │ ├── 15 Minimum Step count.cpp │ ├── 16 Prime Number between Limits.c │ ├── 17 swapping adjacent element.cpp │ ├── 18 reverse a string.cpp │ ├── 19 find out the avg.cpp │ ├── 20 2D Array.cpp │ ├── 21 Dynamically created memory.cpp │ ├── 22 Display append insert delete.cpp │ ├── 23 LS.cpp │ ├── 24 practice.cpp │ ├── 25 practice2.cpp │ ├── 26 practice3.cpp │ ├── 27 Insert in sorted array.cpp │ ├── 28 Arrange Array.cpp │ ├── 29 Pracrice4.cpp │ ├── 30 Merging two sorted Array.cpp │ ├── 31 Array Practice.cpp │ ├── 32 Array Project.cpp │ ├── 33 Multiple count.cpp │ ├── 34 Add two numbers.cpp │ ├── 35 Factorial.cpp │ ├── 36 Practice 4.cpp │ ├── 37 practice.cpp │ ├── 38 Practice.cpp │ ├── 39 Practice whole array.cpp │ ├── 40 Search in a matrix.cpp │ ├── 41 maximum subarray.cpp │ ├── 42 check palindrome.c │ ├── 43 Area of triangle and check paindrome.cpp │ ├── 44 Check anagram.cpp │ └── 45 maximum gap.cpp ├── Readme.md └── Syllabus.pdf ├── Graph └── Basic │ ├── 01 BFS.c │ ├── 02 DFS.c │ ├── 03 PrimMinSpanningTreeFunction.cpp │ ├── 04 KrushalsMinCostSpanningTreeFunction.cpp │ ├── 05 DepthFirstSearchUsingRecursion.cpp │ ├── 06 BreadthFirstSearchUsingSTLQueue.cpp │ └── 07 Breadth First Search Using STLQueue.cpp ├── Hashing Techniques └── Basics │ └── 01 HashTable Chaning.cpp ├── Heap ├── 01 InsertinHeap.cpp ├── 02 CreateHeapUsingSTLVector.cpp ├── 03 Insert in Heap.cpp ├── 04 Delete element from Heap.c ├── 05 Heap sort.c └── 06 HeapifyFunctionFasterMethodtoCreateHeap.cpp ├── LinkedList ├── Basic Consept │ ├── 01 Display LL.c │ ├── 02 Display LL Using Recursion.c │ ├── 03 Count and sum of LL.cpp │ ├── 04 Printing Max element.c │ ├── 05 Display,count,sum,max.cpp │ ├── 06 Searching Element .cpp │ ├── 07 Searching Elements using Recursion & Move to 1st .cpp │ ├── 08 Insertion in linkedlist.cpp │ ├── 09 Circular Linkedlist.cpp │ ├── 10 Insert in sorted ll.c │ ├── 11 project sll .cpp │ ├── 12 Delete from ll.c │ ├── 13 Remove duplicate.c │ ├── 14 Reversing Using Array.c │ ├── 15 Reversing Using Sliding Links.c │ ├── 16 Reversing using Recursion.c │ ├── 17 concatinate linkedlist.c │ ├── 18 Merge LL.c │ ├── 19 Liner Singly Linked List .cpp │ ├── 20 Display Circular LL.c │ ├── 21 Insert in Circular LL.c │ ├── 22 Delete from cll from pos.c │ ├── 23 Delete an element in CLL from pos .cpp │ ├── 24 Create,Display,count DLL.c │ ├── 25 insert at any pos in dll.c │ ├── 26 Delete from DLL.c │ ├── 27 Reverse DLL.c │ ├── 28 Middle of Linkedlist.c │ ├── 29 Polynomial of Linkedlist.c │ └── display ll.cpp └── Problems │ ├── 01 Insertion at the beginning - SLL.c │ ├── 02 Insertion at the end - SLL.c │ ├── 03 Practice.cpp │ └── 04 Practice Linkedlist.cpp ├── Matrix ├── 01 Diagonal matrix.c ├── 01 Diagonal matrix.cpp └── 02 Lower Tringular matrix.c ├── Problems └── Memory Allocation │ ├── 01 create new node.cpp │ ├── 02 Help bob.c │ ├── 03 Dynamic memory allocation.c │ ├── 04 Swapping of no using ptr.c │ └── 05 Finding Square Roots.c ├── Queue ├── 01 Queue using array enqueue & dequeue .cpp ├── 02 C++ Queue using Array.cpp ├── 03 Circular Queue using Array .cpp ├── 04 C++ Circular Queue.cpp ├── 05 Queue using Linkedlist (2).cpp ├── 06 DEQueue using Array .cpp └── 07 Queue using two stack.cpp ├── README.md ├── Recursion ├── 01 Tail recursion.cpp ├── 02 head recursion.cpp ├── 03 global variable recursion.c ├── 04 static variable in reciursion.c ├── 05 Tree recursion.c ├── 06 nested recursion.c ├── 07 Sum of n number using recursion.c ├── 08 Factorial of a numbet using recursion.c ├── 09 Sum Of given number using loop.c ├── 10 powe using recursion.cpp ├── 11 power function.c ├── 12 Taylor Series using Static variables.cpp ├── 13 Taylor Serie honers rule.c ├── 14 Taylor Serie Iterative.cpp ├── 15 Indirect Recursion.c ├── 16 Fibbonaci series using loop and recursion .c ├── 17 fibbonacci using memoization.c ├── 18 nCr formula.c ├── 19 Tower of Hanoi .c ├── ex.c └── ex.cpp ├── Sorting Technique ├── 01 Bubble Sort.c ├── 02 BubbleSort.cpp ├── 03 Insertionsort.c ├── 04 InsertionSort.cpp ├── 05 Selection Sort.c ├── 06 SelectionSort.cpp ├── 07 Mergesort.c ├── 08 MergingFunction.cpp ├── 09 Iterative Mergesort.c ├── 10 IterativeMergeSort.cpp ├── 11 Recursive Mergesort.c ├── 12 RecursiveMergeSort.cpp ├── 13 Count Sort.cpp ├── 14 CountSortFunction.cpp ├── 15 RadixSort Using LL.cpp ├── 16 BinSort Based on LL.cpp ├── 17 Shellsort.cpp └── 18 ShellSort.cpp ├── Stack ├── 01 Operation on stack c++ .cpp ├── 01 Operations on stack .cpp ├── 02 Stack using linkedlist c++.cpp ├── 02 Stack using linkedlist.cpp ├── 03 Parenthesis Balance in C++.cpp ├── 03 Parenthesis checking.cpp ├── 04 infix to postfix.cpp ├── 05 infix to postfix considering Parenthesis and Associativity using Stack.cpp ├── 06 Evaluation of postfix expression.cpp ├── 07 Next Greater Element.cpp ├── 08 Nearest greater to left(NGL).cpp ├── 09 Nearest smaller to left.cpp ├── 10 Nearest smaller to right.cpp ├── 11 Stock span problem.cpp ├── 12 Maximum Rectangular Area in a Histogram.cpp └── 13 Max area rectangle in Binary matrix.cpp ├── String ├── 01 Length of string.cpp ├── 02 Upper to Lower.cpp ├── 03 lower to upper.cpp ├── 04 Upper to Lower & Lower to upper.cpp ├── 05 Vowel & Counstant count.cpp ├── 06 Word Count.cpp ├── 07 Reverse the string.cpp ├── 08 String validation.cpp ├── 09 Compare Two string.cpp ├── 10 Palindrome.cpp └── problem on string │ ├── 01 Length of string.cpp │ ├── 02 Case change.cpp │ ├── 03 String Practice.cpp │ └── 04 Reverse the vowels.cpp ├── Tree ├── AVL │ ├── 01 Rotations in AVL(LL,LR).c │ ├── 02 Rotation in AVL.cpp │ └── 03 Delete From AVL.cpp ├── BST │ ├── 01 BST Insert Inorder & Search.c │ ├── 02 BST Insert,Inorder&Search.cpp │ ├── 03 Insert in BST.c │ ├── 03 Recursive Insert in BST.c │ ├── 04 Delete from BST (Height,InPre,InSucc).c │ └── 05 BST Project.cpp ├── Basic │ ├── 01 Traversal of tree.cpp │ ├── 02 Iterative and recursive traversal of tree .cpp │ ├── 02 Traversal of Tree.c │ ├── 03 Level Order Traversal.c │ ├── 04 Count and Height of tree.c │ ├── 05 Recursive Count and height of tree.c │ ├── 06 Sum of tree.c │ ├── 07 D1,D2,D0(leaf node) count.cpp │ └── 08 leaf,D1,D2 node count.c ├── Problems │ └── 01 Largest in tree.c └── Search Trees │ └── 01 ├── _config.yml └── src ├── Algorithms ├── Divide & Conquer │ ├── 01 Binary Search.cpp │ ├── 02 Quicksort.cpp │ ├── 03 Merge sort.cpp │ └── readme.md ├── Dynamic Programming │ ├── 01 Recursive Knapsack.cpp │ ├── 02 Knapsack Memoization(DP).cpp │ ├── 03 Knapsack Bottom up.cpp │ ├── 04 Subset sum(Knapsack variation).cpp │ ├── 05 Equal sum partition.cpp │ ├── 06 Count of Subsets with given Sum.cpp │ ├── 07 Minimum subset sum difference.cpp │ ├── 08 Count the number of subset with given difference.cpp │ ├── 09 Target sum.cpp │ ├── 10 Unbounded Knapsack.cpp │ ├── 11 Rod cutting problem.cpp │ ├── 12 Coin change problem : maximum no of ways.cpp │ ├── 13 Coin change problem: Minimum number of coin.cpp │ ├── 14 Longest Common Subsequence recursive.cpp │ ├── 15 Longest Common Subsequence Top down(Memoization).cpp │ ├── 16 Longest Common Subsequence Bottom Up(DP).cpp │ ├── 17 LCS Substring.cpp │ ├── 18 Print LCS.cpp │ ├── 19 Shortest Common Supersequence.cpp │ ├── 20 Minimum insertion deletion to convert a to b.cpp │ ├── 21 Longest Palindromic Subsequence.cpp │ ├── 22 Minimum number of deletions to make a string palindrome.cpp │ ├── 23 Print Shortest Common Supersequence.cpp │ ├── 24 Longest repeating subsequence.cpp │ ├── 25 Sequence pattern matching.cpp │ ├── 26 Minimum Number of insertion to make a string palindrome.cpp │ ├── 27 Matrix chain multiplication.cpp │ ├── 28 MCM Memoization.cpp │ ├── 29 Palindrome Partitioning Recursive.cpp │ ├── 30 Palindrome Partitioning Memoization.cpp │ ├── 31 Palindrome Partitioning Memoized optimization.cpp │ ├── 32 Evaluate Expression to true Recursive.cpp │ ├── 33 Evaluate expression to true memoization using map.cpp │ ├── 34 Evaluate expression to true memoization using 3d array.cpp │ ├── 35 Scramble string recursive.cpp │ ├── 36 Scramble string top down.cpp │ ├── 37 Egg dropping problem recursive.cpp │ ├── 38 Egg dropping problem top down.cpp │ ├── 39 Egg dropping problem memoization optimization.cpp │ ├── 40 Diameter of binary tree.cpp │ ├── 41 Max path sum from any node to any.cpp │ ├── 42 Max_path_sum_from_leaf_to_leaf.cpp │ └── readme.md └── l1.txt ├── Competitive Coding (DSA) └── 01 Array │ ├── 01 Maximum SubArray.cpp │ ├── 02 Reverse of array.cpp │ ├── 03 Find the min and max of array.cpp │ ├── 04 Sorting Without sorting algorithm.cpp │ ├── 05 Move all negative numbers to beginning.cpp │ └── 06 Reverse Integer.cpp ├── Hackerrank Contest ├── 01 Mirror Tree.cpp ├── 02 Postorder Tree.c ├── 03 Residue of Execution(using recursion).cpp ├── 04 Negetive Marking.cpp ├── 05 BS.cpp ├── 06 Team Queue.cpp ├── 07 ZZ Stacks- Implementation of Queue.cpp ├── 08 N 501 - Trick or Treat!.cpp ├── 09 In Search of a Number.cpp ├── 10 Greatest Number with the Digits.py ├── 11 Zig Zag traversal.cpp └── 12 Interesting Series.cpp ├── Img ├── DP.png └── DSA.png ├── TC ├── Graph Algorithm.png ├── Graph and heap.png ├── Order of TC.png ├── Sorting Algo.png ├── TC of DS.png └── Time Complexity problems │ └── readme.md └── cpp ├── ** inheritence.cpp ├── 00 basic.cpp ├── 00 basic1.cpp ├── 00 basic2.cpp ├── 00 basic3.cpp ├── 00 basic4.cpp ├── 01 Function ├── 01 Function Overloading.cpp ├── 02 Function Template.cpp ├── 03 Default Argument.cpp └── Simple exaample.cpp ├── 01 Referance vaiable & Typecasting.cpp ├── 02 Constants, Manipulators & Operator Precedence.cpp ├── 02 OOPS concept ├── 01 Basic class & Obj.cpp ├── 02 Area & perimeter of rect.cpp ├── 03 pointer to an object.cpp ├── 04 Data Abstraction(Accessor & Mutator ).cpp ├── 05 Constructor.cpp ├── 06 All types of the fxn.cpp ├── 07 this function.cpp ├── 08 Struct & Class.cpp ├── 09 Students details.cpp ├── 10 class and obj.cpp └── 11 constructor.cpp ├── 03 If Else and Switch-Case Statement.cpp ├── 04 Loops For while do while .cpp ├── 05 Break and Continue.cpp ├── 06 Pointers.cpp ├── 07 Array & Pointers Arithmetic.cpp ├── 08 Struct Union Enum.cpp ├── 09 Function & Function Prototypes.cpp ├── 10 Call by value & Call by Ref.cpp ├── 11 inline function default argument & const arguments.cpp ├── 12 Recursion and Recursive fxn.cpp ├── 13 Function Overloding.cpp ├── 14 OOP Class and object.cpp ├── 15 OOPs Recap & Nesting of Member Functions.cpp ├── 16 Objects Memory Allocation & using Arrays in Classes.cpp ├── 17 Static Data Members & Methods.cpp ├── 18 Array of Objects & Passing Objects as Function Arguments.cpp ├── 19 Friend Functions.cpp ├── 20 Friend Classes & Member Friend Functions.cpp ├── 21 More on C++ Friend Functions.cpp ├── 22 Constructors.cpp ├── 23 Paramererized Constructor.cpp ├── 24 Parameterized Constructor.cpp ├── 25 Constructor Overloading.cpp ├── 26 Constructor with dafults Arguments.cpp ├── 27 Dynamic Initization of obj using constructor.cpp ├── 28 Copy Constructor.cpp ├── 29 Destructor.cpp ├── 30 Inheritance Syntax.cpp ├── 31 Single Inheritance.cpp ├── 32 Protected Access Modifier.cpp ├── 33 Multilevel Inheritance.cpp ├── 34 Multiple Inheritance.cpp ├── 35 Exercise on Inheritance.cpp ├── 36 Ambiguity Resolution in Inheritance.cpp ├── 37 Virtual Base Class.cpp ├── 38 Constructors in Derived Class.cpp ├── 39 Initialization list in Constructors.cpp ├── 40 pointers.cpp ├── 41 Pointers to Objects and Arrow Operator.cpp ├── 42 Array of Objects Using Pointers.cpp ├── 43 this Pointer.cpp ├── 44 Polymorphism.cpp ├── 45 Virtual Functions.cpp ├── 46 Virtual Functions Example.cpp ├── 47 Abstract Base Class & Pure Virtual Functions.cpp ├── 48 File I or O.cpp ├── Cpp_programs.md ├── Cpp_things.md ├── STL ├── 01 Generic introduction.cpp ├── 02 Generic class using template.cpp ├── 03 Bubble sort using template.cpp ├── 04 Our Monk loves candy!.cpp ├── 05 print string in lexicographical order.cpp ├── 06 Map.cpp ├── Containers │ └── Vector │ │ ├── 01 Begin fun.cpp │ │ ├── 02 end fun.cpp │ │ ├── 03 Maps creation.cpp │ │ ├── 04 print map & its size.cpp │ │ ├── 05 map.find, map.erase, m.clear.cpp │ │ └── 06 unordered_map.cpp └── Readme.md └── basic.txt /Array/01 static and dynamic array .c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int A[5] = { 1,2,3,4,5 }; // this is allocated inside stack memory 6 | int* p; // pointer of array 7 | int i; 8 | p = (int*)malloc(5 * sizeof(int)); // dynamically createing array inside heap 9 | p[0] = 1; // initializing all the terms in array 10 | p[1] = 2; 11 | p[2] = 3; 12 | p[3] = 4; 13 | p[4] = 5; 14 | 15 | for (i = 0; i < 5; i++) // iteration for normal array 16 | { 17 | printf("%d ", A[i]); 18 | } 19 | printf("\n"); 20 | for (i = 0; i < 5; i++) // iteration for the dynamic array 21 | { 22 | printf("%d ",p[i]); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Array/02 increasing size of array using heap.cpp: -------------------------------------------------------------------------------- 1 | /* Normal array is created in stack 2 | and stack used memory array cannot be change 3 | tht is we can not incresae the size of array */ 4 | 5 | #include 6 | #include 7 | int main () 8 | { 9 | int *p,*q; // Taking two pointers 10 | int i; 11 | p=(int *)malloc(5*sizeof(int)); // array is created in heap memory 12 | p[0]=9;p[1]=7;p[2]=22;p[3]=11;p[4]=12; 13 | 14 | q= (int *) malloc(10*sizeof(int)); // increasing size of array in heap memory 15 | 16 | for (i=0;i<5;i++) 17 | q[i]=p[i]; 18 | 19 | free(p); 20 | p=q; 21 | q=NULL; 22 | 23 | for (i=0;i<5;i++) 24 | printf("%d \n",p[i]); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Array/03 Creating 2D array & Accessing.c: -------------------------------------------------------------------------------- 1 | // Creating 2-D array 2 | #include 3 | #include 4 | int main () 5 | { // normal delaration and initialization 6 | int A[3][4]={{1,2,3,4},{2,4,6,8},{1,3,5,7}}; // stack memory 7 | 8 | // pointer decleration 9 | int *B[3]; 10 | int **C; // double pointer , completly in heap memory 11 | int i,j; 12 | 13 | B[0]=(int *)malloc(4*sizeof(int)); // Creating memory in heap 14 | B[1]=(int *)malloc(4*sizeof(int)); 15 | B[2]=(int *)malloc(4*sizeof(int)); 16 | 17 | C=(int **)malloc(3*sizeof(int *)); 18 | C[0]=(int *)malloc(4*sizeof(int)); 19 | C[1]=(int *)malloc(4*sizeof(int)); 20 | C[2]=(int *)malloc(4*sizeof(int)); 21 | 22 | for (i=0;i<3;i++) 23 | { 24 | for (j=0;j<4;j++) 25 | printf("%d ",A[i][j]); 26 | printf("\n"); 27 | } 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Array/04 Array ADT.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Array 4 | { 5 | int* A; 6 | int size; 7 | int length; 8 | }; 9 | 10 | // fxn for display all the elements of the array 11 | 12 | void Display(struct Array arr) 13 | { 14 | int i; 15 | printf("Elements Are\n"); 16 | for (i = 0; i < arr.length; i++) 17 | printf("%d ",arr.A[i]); // it will display all the elements 18 | } 19 | int main() 20 | { 21 | struct Array arr; 22 | int n; // how many no is going to be inserted 23 | int i; 24 | printf("Enter size of an array "); 25 | scanf_s("%d", &arr.size); 26 | arr.A = (int*)malloc(arr.size * sizeof(int)); // dynamically created array 27 | arr.length = 0;// intially no elements 28 | printf("Enter number of numbers "); 29 | scanf_s("%d", &n); 30 | printf("Enter All the elements "); 31 | for (i = 0; i < n; i++) 32 | scanf_s("%d", &arr.A[i]); // enter the elements of an array 33 | arr.length = n; // set length as n 34 | 35 | 36 | Display(arr); 37 | 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Array/07 Linear search.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct Array { 3 | int A[20]; 4 | int length; 5 | int size; 6 | 7 | }; 8 | void Display (struct Array arr) 9 | { 10 | int i; 11 | printf("\nEnter elements\n"); 12 | for (i=0;i 2 | struct Array { 3 | int A[20]; 4 | int length; 5 | int size; 6 | 7 | }; 8 | void Display (struct Array arr) 9 | { 10 | int i; 11 | printf("\nEnter elements\n"); 12 | for (i=0;ilength;i++) // iterating through the list 25 | { 26 | if(key==arr->A[i]) // checking whether elements is equal to the key elements or not 27 | { 28 | swap(&arr->A[i],&arr->A[0]); // this is move to 1st method // from this we can reduce time to O(1) // when we will search the same element again 29 | return i; 30 | } 31 | } 32 | return -1; 33 | } 34 | 35 | int main () 36 | { 37 | struct Array arr1= {{2,3,4,2,3,4,5,88,9 },20,5 }; 38 | printf("The element is at %dth index",LinearSearch(&arr1,9)); 39 | Display(arr1); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Array/09 Binary Search using loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct Array 3 | { 4 | int A[10]; 5 | int size; 6 | int length; 7 | }; 8 | void Display(struct Array arr) 9 | { 10 | int i; 11 | printf("\nElements are\n"); 12 | for(i=0;i 2 | struct Array { 3 | int A[10]; 4 | int length; 5 | int size; 6 | 7 | }; 8 | void Display (struct Array arr) 9 | { 10 | int i; 11 | printf("Enter all the elements "); 12 | for (i=0;i 2 | 3 | struct Array 4 | { 5 | int A[20]; 6 | int length; 7 | int size; 8 | }; 9 | void Display (struct Array arr) 10 | { 11 | int i; 12 | for (i=0;isize]; // creating the required size of array in heap 28 | for (i = arr->length - 1, j = 0; i >= 0; i--, j++) // keeping all the elements from array a to b in reverse order 29 | B[j] = arr->A[i]; // coping element everytime 30 | for (i = 0; i < arr->length; i++) 31 | arr->A[i] = B[i]; // copying back elements to A 32 | } 33 | 34 | void Reverse1(struct Array* arr) 35 | { 36 | int i, j; 37 | for (i = 0, j = arr->length - 1; i < j; i++, j--) // we will interchange 1st with the last element 38 | Swap(&arr->A[i], &arr->A[j]); // swapping 1st with the last 39 | 40 | } 41 | 42 | 43 | int main() 44 | 45 | { 46 | struct Array arr1={{2,3,4,56,66,69,98},7,20}; 47 | // Call any one method to execute 48 | Reverse(&arr1); // Shifting 49 | Reverse1(&arr1); // 2nd method by swapping 1st with last i.e rotating 50 | Display(arr1); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Array/13 IsSorted .c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Array 5 | { 6 | int* A; 7 | int size; 8 | int length; 9 | }; 10 | void Display(struct Array* arr) 11 | { 12 | int i; 13 | cout << "the elements of the array is " << endl; 14 | for (i = 0; i < arr->length; i++) 15 | cout <A[i] << " "; 16 | } 17 | 18 | int IsSorted(struct Array* arr) 19 | { 20 | for (int i = 0; i < arr->length - 1; i++) 21 | { 22 | if (arr->A[i] > arr->A[i + 1]) // if 1st element is greater then 2nd element then it will k/as not sorted so return 0 23 | return 0; 24 | } 25 | 26 | return 1; // if everything is ok and the element came successfully out of loop means it is sorted return 1 for thet 27 | } 28 | int main() 29 | { 30 | struct Array arr; 31 | cout << "Enter the size of Array " << endl; 32 | cin >> arr.size; 33 | arr.A = new int[arr.size]; 34 | int no; 35 | cout << "Enter the length of the array " << endl; 36 | cin >> no; 37 | arr.length = 0; 38 | cout << "Enter the elements of the array " << endl; 39 | for (int i = 0; i < no; i++) 40 | cin >> arr.A[i]; 41 | arr.length = no; 42 | cout< 2 | 3 | struct Array 4 | { 5 | int A[20]; 6 | int length; 7 | int size; 8 | }; 9 | void Display (struct Array arr) 10 | { 11 | int i; 12 | for (i=0;ilength-1; 20 | if (arr->length==arr->size) return ; 21 | while (i>=0 && arr->A[i]>x) 22 | { 23 | arr->A[i+1]=arr->A[i]; 24 | i--; 25 | } 26 | 27 | 28 | 29 | arr->A[i+1]=x; 30 | 31 | arr->length++; 32 | 33 | } 34 | 35 | 36 | int main() 37 | 38 | { 39 | struct Array arr={{2,3,5,10,15},5,20}; 40 | InsertSort(&arr,1); 41 | Display(arr); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Array/19 Single Missing Element in Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Array { 4 | int* A; 5 | int size; 6 | int length; 7 | }; 8 | void SingleMissingElement(struct Array* arr) { 9 | int sum = 0; 10 | int n = arr->A[arr->length-1]; // this denotes the last element of the array 11 | for (int i = 0; i < arr->length; i++) 12 | { 13 | sum += arr->A[i]; // sum of the existing array 14 | } 15 | // (n*(n+1))/2 ----> Gives the total sum upto that number 16 | int target = (n * (n + 1)) / 2- sum; 17 | cout << "The missing number is " << target; 18 | 19 | } 20 | int main() { 21 | struct Array arr; 22 | int no; 23 | cout << "Enter the size of the array " << endl; 24 | cin >> arr.size; 25 | arr.A = new int[arr.size]; 26 | arr.length = 0; 27 | cout << "Enter the size of the array" << endl; 28 | cin >> no; 29 | cout << "Enter the elements of the array " << endl; 30 | for (int i = 0; i < no; i++) 31 | cin >> arr.A[i]; 32 | arr.length = no; 33 | SingleMissingElement(&arr); 34 | return 0; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Array/21 Multiple Missing Elements in Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | //initialising diff with first index element of array 2 | 3 | //We will check for the differnce between the index element and the array index 4 | //Wherever the difference will not be equal, a missing element is there 5 | //will increment diff till the difference a[i]-i not equal to diff to continue as soon as we find the missing element 6 | 7 | #include 8 | using namespace std; 9 | struct Array { 10 | int* A; 11 | int size; 12 | int length; 13 | }; 14 | void MultipleMissingElement(struct Array* arr) { 15 | int low = arr->A[0]; 16 | int difference = low - 0; 17 | for (int i = 0; i < arr->length; i++) { 18 | if (arr->A[i] - i != difference) { 19 | while (difference < arr->A[i] - i) 20 | { 21 | cout << i + difference<<" "; 22 | difference++; 23 | } 24 | } 25 | } 26 | } 27 | 28 | int main() { 29 | struct Array arr; 30 | int no; 31 | cout << "Enter the size of the array " << endl; 32 | cin >> arr.size; 33 | arr.A = new int[arr.size]; 34 | arr.length = 0; 35 | cout << "Enter the size of the array" << endl; 36 | cin >> no; 37 | cout << "Enter the elements of the array " << endl; 38 | for (int i = 0; i < no; i++) 39 | cin >> arr.A[i]; 40 | arr.length = no; 41 | 42 | cout << "The missing elements are ! " << endl; 43 | MultipleMissingElement(&arr); 44 | return 0; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Array/25 b. Find Duplicate in unsorted array using hash table.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Array { 4 | int* A; 5 | int size; 6 | int length; 7 | }; 8 | 9 | 10 | void Count_duplicate_unsorted_hash(struct Array* arr) { 11 | int low = Min(arr); 12 | int high = Max(arr); 13 | int* H; 14 | H = new int[high];; 15 | for (int i = 0; i < high; i++) H[i]={ 0 }; // initialise hash table with zero 16 | for (int i = 0; i < arr->length; i++) 17 | H[arr->A[i]]++; // for every element go to that particular index and make increment everytime 18 | for (int i = low; i <= high; i++) // traverse through the whole hash table and check the elements 19 | { 20 | 21 | if (H[i] > 1) // if hash table elements is greater then 1 that is there is the duplicate element in the array 22 | cout << i << " is appearing for " << H[i] << " Times" << endl; // print the duplicate and also print its frequency 23 | } 24 | 25 | } 26 | 27 | int main() { 28 | struct Array arr; 29 | int no; 30 | cout << "Enter the size of the array " << endl; 31 | cin >> arr.size; 32 | arr.A = new int[arr.size]; 33 | arr.length = 0; 34 | cout << "Enter the size of the array" << endl; 35 | cin >> no; 36 | cout << "Enter the elements of the array " << endl; 37 | for (int i = 0; i < no; i++) 38 | cin >> arr.A[i]; 39 | arr.length = no; 40 | 41 | Count_duplicate_unsorted_hash(&arr); 42 | 43 | 44 | // Display(arr); 45 | return 0; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Array/27 Find a pair with sum k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main () 4 | { 5 | int A[10]={1,2,4,5,6,8,10,12,14,15}; 6 | int n =sizeof(A)/sizeof(int); 7 | int k=10 ;// sum of pair of element is 10 8 | int i,j; 9 | i=0; 10 | j=n-1; 11 | while (i1) // if the index is greater then 1 that means it is a duplicate element 14 | res[0]=arr[i]; 15 | if (freq[i]==0) // the index which will be having 0 will be the missing number 16 | res[1]=i; 17 | } 18 | // this is edge case when result's 1st index will be zero i.e last number is missing 19 | /* For example 20 | Possibly your code doesn't work correctly for multiple test-cases (TCs). 21 | 22 | The first test case where your code failed: 23 | 24 | Input: 25 | 14 26 | 12 7 5 1 13 1 10 8 11 9 2 4 3 6 27 | 28 | Its Correct output is: 29 | 1 14 30 | 31 | And Your Code's output is: 32 | 1 0 33 | */ 34 | if (res[1]==0) 35 | res[1]=n; 36 | return res; 37 | } 38 | }; 39 | 40 | // O(2n)tc 41 | // o(n)sc 42 | -------------------------------------------------------------------------------- /Array/Geeksforgeeks problem/05 Merge Without Extra Space.cpp: -------------------------------------------------------------------------------- 1 | int nextGap(int gap){ 2 | if (gap <= 1){ 3 | return 0; 4 | } 5 | return (gap / 2) + (gap % 2); 6 | } 7 | 8 | void merge(int arr1[], int arr2[], int n, int m) { 9 | 10 | int gap = n + m; // we took gap as sum sum the size of both the array 11 | int i, j; 12 | 13 | gap = nextGap(gap); // everytime we will get a next gap from this 14 | for(; gap > 0 ; gap = nextGap(gap) ){ 15 | 16 | //Comparing elements in first array arr1[] 17 | for(i = 0; i + gap < n; i++){ 18 | if(arr1[i] > arr1[i+gap]) // whenever element in 1st array is greater then 2nd array swap the element and move ahead 19 | swap(arr1[i], arr1[i+gap]); 20 | } 21 | 22 | //Comparing elements in both array arr1[] and arr2[] 23 | for(j = (gap > n) ? (gap - n) : 0; i < n && j < m; i++, j++ ){ 24 | if(arr1[i] > arr2[j]) // whenever element in 1st array is greater then 2nd array swap the element and move ahead 25 | swap(arr1[i], arr2[j]); 26 | } 27 | 28 | //Comparing elements in second array arr2[] 29 | if(j < m){ 30 | for(j = 0; j + gap < m; j++){ 31 | if(arr2[j] > arr2[j + gap]) 32 | swap(arr2[j], arr2[j+gap]); 33 | } 34 | } 35 | } 36 | } 37 | 38 | //tc: O(n*logn), sc:O(1) 39 | -------------------------------------------------------------------------------- /Array/Geeksforgeeks problem/string reverse.cpp: -------------------------------------------------------------------------------- 1 | string reverseWord(string str){ 2 | 3 | string rev = ""; 4 | for(int i = str.size()-1;i>=0 ;i--){ 5 | rev += str[i]; 6 | } 7 | 8 | return rev; 9 | 10 | } 11 | 12 | =========================================================================== 13 | -------------------------------------------------------------------------------- /Array/Problems/04 Maximum Index.cpp: -------------------------------------------------------------------------------- 1 | /* Given an array A of integers, find the maximum of j - i subjected to the constraint of A[i] <= A[j]. 2 | 3 | Example : 4 | 5 | A : [3 5 4 2] 6 | 7 | Output : 2 8 | for the pair (3, 4) 9 | 10 | 11 | Input: 12 | 13 | The first line contains an integer T, depicting total number of test cases. 14 | Then following T lines contains an integer N depicting the size of array and next line followed by the value of array. 15 | 16 | 17 | Output: 18 | 19 | Print the maximum difference of the indexes i and j in a separate line. 20 | 21 | 22 | Constraints: 23 | 24 | 1<=T<=30 25 | 1<=N<=1000 26 | 0<=A[i]<=100 27 | 28 | 1 29 | 2 30 | 1 10 31 | 1 32 | 1 33 | 4 34 | 2 5 7 9 35 | 3 36 | 37 | 38 | Code by @author Shivendra K jha 39 | 40 | */ 41 | #include 42 | using namespace std; 43 | int main() 44 | { int t,a[1000]; 45 | cin>>t; 46 | 47 | while(t--){ 48 | int n,max=-1,i=0; 49 | cin>>n; 50 | int n1=n; 51 | while(n--) 52 | { 53 | cin>>a[i]; 54 | i++; 55 | } 56 | for(int i=0;imax) max = j-i; 63 | } 64 | } 65 | 66 | } 67 | cout< 19 | using namespace std ; 20 | int main () 21 | { 22 | int i,no,A[10],sum=0,avg; 23 | cin>>no; // number of elements in an araay 24 | for (i=0;i>A[i]; 27 | sum+=A[i]; 28 | avg=sum/no; // taking mean of array 29 | } 30 | cout< 26 | using namespace std; 27 | int main () 28 | { 29 | int m,n,i,j,A[10][10],S[10][10]; 30 | cin>>m>>n; 31 | for (i=0;i>A[i][j]; 35 | } 36 | int p,q,B[10][10]; 37 | cin>>p>>q; 38 | for (i=0;i>B[i][j]; 42 | } 43 | 44 | for (i=0;i 27 | using namespace std; 28 | int main () 29 | { 30 | int no,i,j,count=1; 31 | int A[100]; 32 | cin>>no; 33 | for (i=0;i>A[i]; 35 | 36 | for(i=0;i=0) 46 | cout< 2 | using namespace std; 3 | int main () 4 | { 5 | int length,width; 6 | 7 | cin>>length>>width; 8 | int prm= 2*(length+width); 9 | int area = length*width ; 10 | cout << "Length= "< 18 | using namespace std; 19 | int main () 20 | { 21 | int i,A[10],no; 22 | int min=A[0]; 23 | cin>>no; 24 | for(i=0;i>A[i]; 27 | if (A[i]>min) 28 | { 29 | min=A[i]; 30 | 31 | } 32 | } 33 | cout<<"Largest element= "< 16 | using namespace std; 17 | int main() 18 | { 19 | int i,n,temp; 20 | cin>>n; 21 | int a[n]; 22 | for(i=0;i>a[i]; 24 | } 25 | for(i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int a1[100],a2[100],a3[100],index=0; 6 | int n1,n2,m,i; 7 | cout<<"enter the number of elements in array 1 "; 8 | cin>>n1; 9 | cout<<"enter the elements of 1st array "; 10 | for (i=0;i>a1[i]; 13 | } 14 | cout<<"Enter the number of elements in array 2 "; 15 | cin>>n2; 16 | cout<<"enter the elements of 2nd array "; 17 | for (i=0;i>a2[i]; 19 | m=n1+n2; 20 | for (i=0;i median 5 (5) 9 | 15 goes to stream --> median 10 (5, 15) 10 | 1 goes to stream --> median 5 (5, 15, 1) 11 | 3 goes to stream --> median 4 (5, 15, 1, 3) 12 | 13 | Input: 14 | The first line of input contains an integer N denoting the no of elements of the stream. Then the next N lines contains integer x denoting the no to be inserted to the stream. 15 | 16 | Output: 17 | For each element added to the stream print the floor of the new median in a new line. 18 | 19 | Constraints: 20 | 1<=N<=10^5+7 21 | 1<=x<=10^5+7 22 | 4 23 | 5 24 | 15 25 | 1 26 | 3 27 | 5 28 | 10 29 | 5 30 | 4 31 | 4 32 | 8 33 | 20 34 | 2 35 | 5 36 | 8 37 | 14 38 | 8 39 | 6 40 | 41 | */ 42 | 43 | #include 44 | int main() 45 | { int n,i; 46 | 47 | scanf("%d",&n); 48 | int arr[n]; 49 | for(i=0;i 19 | using namespace std; 20 | int main () 21 | { 22 | int no,i,temp; 23 | cin>>no; 24 | int a[no]; 25 | for (i=0;i>a[i]; 27 | for(i=0;i 2 | using namespace std; 3 | int main() { 4 | int t,no,i; 5 | cin>>t; 6 | for(i=0;i>no; 8 | char s[no]; 9 | char rs[no]; 10 | cin>>s; 11 | for (i=0;s[i]!='\0';i++) 12 | { 13 | 14 | } 15 | i=i-1; 16 | for (int j=0;i>=0;i-=1,j++) 17 | { 18 | rs[j]=s[i]; 19 | } 20 | rs[i]='\0'; 21 | cout< 4 | using namespace std; 5 | int main() 6 | { 7 | int arr[10]; 8 | float sum = 0; 9 | float avg; 10 | int no; 11 | cin >> no; 12 | /* We are using a for loop to traverse through the array 13 | * while storing the entered values in the array 14 | */ 15 | for (int i = 0; i < no; i++) 16 | { 17 | cout << "Enter the pos " << i + 1 << endl; 18 | cin >> arr[i]; 19 | } 20 | for (int i = 0; i < no; i++) { 21 | sum = sum + arr[i]; 22 | 23 | } 24 | avg = sum / no; 25 | cout << endl; 26 | cout <<"The avg of all the number is "<< avg; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Array/Problems/20 2D Array.cpp: -------------------------------------------------------------------------------- 1 | // Two dimensional(2D) Array 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int arr[3] [3]; 8 | int i, j; 9 | for (i = 0; i < 3; i++) 10 | { 11 | for (j = 0; j < 3; j++) 12 | { 13 | printf("Enter the term to be displayed [%d] [%d] : ",i,j); 14 | scanf_s("%d",&arr[i][j]); 15 | } 16 | } 17 | // Display Array elements 18 | printf("Two dimension elements are : \n "); 19 | for (i = 0; i < 3; i++) 20 | { 21 | for (j = 0; j < 3; j++) { 22 | printf("%d ", arr[i][j]); 23 | if (j == 2) 24 | printf("\n"); 25 | } 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Array/Problems/21 Dynamically created memory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int A[6] = { 1,2,3,4,4,5 }; 6 | int* p; 7 | int i; 8 | p = new int[4]; // dynamically created memory 9 | p[0] = 1; p[1] = 2; p[2] = 3; p[3] = 9; 10 | for (i = 0; i < 6; i++) 11 | cout << A[i]<<" "; 12 | cout << endl; 13 | for (i = 0; i < 4; i++) { 14 | 15 | cout << p[i] << " "; 16 | } 17 | return 0; 18 | 19 | } -------------------------------------------------------------------------------- /Array/Problems/27 Insert in sorted array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Array 5 | { 6 | int* A; 7 | int size; 8 | int length; 9 | }; 10 | 11 | void Display(struct Array* arr) 12 | { 13 | int i; 14 | cout << "the elements of the array is " << endl; 15 | for (i = 0; i < arr->length; i++) 16 | cout <A[i] << " "; 17 | } 18 | void Insert1(struct Array* arr, int val) 19 | { 20 | int i; 21 | i = arr->length - 1; 22 | while (arr->A[i] > val) // here we dont know how many times we have to repeat the loop so we used while loop 23 | { 24 | arr->A[i + 1] = arr->A[i]; // go on shifting the elements value is less then array's elements 25 | i--; 26 | } 27 | arr->A[i + 1] = val; // set the value at a given pos 28 | arr->length++; 29 | } 30 | int main() 31 | { 32 | struct Array arr; 33 | cout << "Enter the size of Array " << endl; 34 | cin >> arr.size; 35 | arr.A = new int[arr.size]; 36 | int no; 37 | cout << "Enter the length of the array " << endl; 38 | cin >> no; 39 | arr.length = 0; 40 | cout << "Enter the elements of the array " << endl; 41 | for (int i = 0; i < no; i++) 42 | cin >> arr.A[i]; 43 | arr.length = no; 44 | Insert1(&arr, 18); 45 | Display(&arr); 46 | 47 | 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Array/Problems/28 Arrange Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Array 5 | { 6 | int* A; 7 | int size; 8 | int length; 9 | }; 10 | 11 | void Display(struct Array* arr) 12 | { 13 | int i; 14 | cout << "the elements of the array is " << endl; 15 | for (i = 0; i < arr->length; i++) 16 | cout <A[i] << " "; 17 | } 18 | 19 | void Arrange(struct Array* arr) 20 | { 21 | int i=0; 22 | int j = arr->length - 1; // set j to the last of the array element 23 | while (i < j) { 24 | while (arr->A[i] < 0) { i++; } //while the array element is less then zero keep increamenting the array 25 | while (arr->A[j] >= 0) { j--; } // while the array elemet is greater then zero decreamenting the array from last 26 | if (i < j) // and when i is less then j interchange the value 27 | Swap(&arr->A[i], &arr->A[j]); 28 | } 29 | // time taken is O (n) -- > comparing the elemets total n+2 elements compared 30 | } 31 | 32 | 33 | int main() 34 | { 35 | struct Array arr; 36 | cout << "Enter the size of Array " << endl; 37 | cin >> arr.size; 38 | arr.A = new int[arr.size]; 39 | int no; 40 | cout << "Enter the length of the array " << endl; 41 | cin >> no; 42 | arr.length = 0; 43 | cout << "Enter the elements of the array " << endl; 44 | for (int i = 0; i < no; i++) 45 | cin >> arr.A[i]; 46 | arr.length = no; 47 | Arrange(&arr); 48 | Display(&arr); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Array/Problems/33 Multiple count.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Input: 3 | 7 3 4 | 1 5 | 51 6 | 966369 7 | 7 8 | 9 9 | 999996 10 | 11 11 | 12 | Output: 13 | 4 14 | */ 15 | 16 | #include 17 | using namespace std; 18 | 19 | 20 | int main() 21 | { 22 | int n, k; 23 | cin >> n >> k; 24 | 25 | int A[n]; 26 | 27 | int ans = 0; 28 | for (int i = 0; i < n; i++) 29 | { 30 | cin >> A[i]; 31 | if (A[i] % k == 0) 32 | { 33 | ans++; 34 | } 35 | } 36 | cout << ans; 37 | return 0; 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Array/Problems/34 Add two numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; 6 | cin >> t; 7 | while (t--) 8 | { 9 | int a, b; 10 | cin >> a >> b; 11 | int c = a + b; 12 | cout << c< 18 | using namespace std; 19 | 20 | int fact(int n) 21 | { 22 | if (n<=1) 23 | return 1; 24 | else 25 | return n*fact(n-1); 26 | } 27 | 28 | int main() { 29 | int t; 30 | cin>>t; 31 | while (t--) 32 | { 33 | int a; 34 | cin>>a; 35 | cout< 2 | using namespace std; 3 | struct Array { 4 | int* A; 5 | int size; 6 | int length; 7 | }; 8 | 9 | void Display(struct Array arr) { 10 | cout << "The elements of the array is !" << endl; 11 | for (int i = 0; i < arr.length; i++) 12 | cout << arr.A[i]<<" "; 13 | } 14 | 15 | void Append(struct Array *arr, int x) { 16 | if (arr->length < arr->size) { 17 | arr->A[arr->length] = x; 18 | arr->length++; 19 | } 20 | } 21 | 22 | void Insert(struct Array* arr, int index, int val) { 23 | for (int i = arr->length; i > index; i--) 24 | arr->A[i] = arr->A[i - 1]; 25 | arr->A[index] = val; 26 | arr->length++; 27 | } 28 | 29 | 30 | 31 | int main() { 32 | struct Array arr; 33 | int no; 34 | cout << "Enter the size of the array " << endl; 35 | cin >> arr.size; 36 | arr.A = new int[arr.size]; 37 | arr.length = 0; 38 | cout << "Enter the size of the array" << endl; 39 | cin >> no; 40 | cout << "Enter the elements of the array " << endl; 41 | for (int i = 0; i < no; i++) 42 | cin >> arr.A[i]; 43 | arr.length = no; 44 | 45 | // Append(&arr, 5); 46 | //Insert(&arr, 2, 7); 47 | Display(arr); 48 | return 0; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Array/Problems/40 Search in a matrix.cpp: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/search-in-a-matrix17201720/1# 2 | class Solution{ 3 | public: 4 | int matSearch (vector > &mat, int N, int M, int X) 5 | { 6 | int i=0; 7 | int j=M-1; 8 | while ((i>=0 && i=0 && jX) 14 | { 15 | j--; 16 | } 17 | else 18 | { 19 | i++; 20 | } 21 | } 22 | return 0; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Array/Problems/41 maximum subarray.cpp: -------------------------------------------------------------------------------- 1 | // kadane algo says that we will carry a sum until it gives the positive sum 2 | class Solution { 3 | public: 4 | int maxSubArray(vector& nums) { 5 | int sum=0; 6 | int maxi=INT_MIN; 7 | for (auto i:nums) 8 | { 9 | sum+=i; 10 | maxi=max(maxi,sum); // everytime take maximum after adding the next element 11 | if (sum<0) sum=0; // whenever sum become less then 0 set its value to zero 12 | } 13 | return maxi; 14 | } 15 | }; 16 | // tc sc =O(n), O(1) 17 | -------------------------------------------------------------------------------- /Array/Problems/42 check palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,r,sum=0,temp; 5 | printf("enter the number="); 6 | scanf("%d",&n); 7 | temp=n; 8 | while(n>0) 9 | { 10 | r=n%10; // getting the reminder 11 | sum=(sum*10)+r; // storing in sum 12 | n=n/10; // redusing the number 13 | } 14 | if(temp==sum) 15 | printf("palindrome number "); 16 | else 17 | printf("not palindrome"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Array/Problems/43 Area of triangle and check paindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | // Try out your code here 8 | int a,b,c,d,s,area; 9 | cin>>a>>b>>c; 10 | s=(a+b+c)/2; 11 | d=s*(s-a)*(s-b)*(s-c); 12 | area=sqrt(d); 13 | cout<<"Area of a triangle = "<0){ 17 | r=area%10; 18 | sum=sum*10+r; 19 | area=area/10; 20 | } 21 | if (temp==sum) 22 | { 23 | cout<<"Area is palindrome"; 24 | } 25 | else 26 | { 27 | cout<<"Area is not palindrome"; 28 | } 29 | 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Array/Problems/44 Check anagram.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to check if the strings are anagrams 2 | 3 | #include 4 | using namespace std; 5 | 6 | // function to check whether two strings are anagram of each other 7 | bool areAnagram(string str1, string str2) 8 | { 9 | // Get lengths of both strings 10 | int n1 = str1.length(); 11 | int n2 = str2.length(); 12 | 13 | // If length of both strings is not same, then they 14 | // cannot be anagram 15 | if (n1 != n2) 16 | return false; 17 | 18 | // Sort both the strings 19 | sort(str1.begin(), str1.end()); 20 | sort(str2.begin(), str2.end()); 21 | 22 | // Compare sorted strings 23 | for (int i = 0; i < n1; i++) 24 | if (str1[i] != str2[i]) 25 | return false; 26 | 27 | return true; 28 | } 29 | 30 | int main() 31 | { 32 | string str1; 33 | string str2; 34 | cout << “\nInput the strings : “; 35 | cin >> str1 >> str2; 36 | if (areAnagram(str1, str2)) 37 | cout << “The two strings are anagram of each other”; 38 | else 39 | cout << “The two strings are not anagram of each other”; 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Array/Problems/45 maximum gap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int maxSortedAdjacentDiff(int* arr, int n) 4 | { 5 | int maxVal = arr[0], minVal = arr[0]; 6 | for (int i = 1; i < n; i++) { 7 | maxVal = max(maxVal, arr[i]); 8 | minVal = min(minVal, arr[i]); 9 | } 10 | int maxBucket[n - 1]; 11 | int minBucket[n - 1]; 12 | fill_n(maxBucket, n - 1, INT_MIN); 13 | fill_n(minBucket, n - 1, INT_MAX); 14 | float delta = (float)(maxVal - minVal) / (float)(n - 1); 15 | for (int i = 0; i < n; i++) { 16 | if (arr[i] == maxVal || arr[i] == minVal) 17 | continue; 18 | int index = (float)(floor(arr[i] - minVal) / delta); 19 | maxBucket[index] = max(maxBucket[index], arr[i]); 20 | minBucket[index] = min(minBucket[index], arr[i]); 21 | } 22 | int prev_val = minVal; 23 | int max_gap = 0; 24 | for (int i = 0; i < n - 1; i++) { 25 | if (minBucket[i] == INT_MAX) 26 | continue; 27 | max_gap = max(max_gap, minBucket[i] - prev_val); 28 | prev_val = maxBucket[i]; 29 | } 30 | max_gap = max(max_gap, maxVal - prev_val); 31 | 32 | return max_gap; 33 | } 34 | int main() 35 | { 36 | int n; 37 | cin>>n; 38 | int arr[n]; 39 | for (int i=0;i>arr[i]; 42 | } 43 | cout << maxSortedAdjacentDiff(arr, n) << endl; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Array/Readme.md: -------------------------------------------------------------------------------- 1 | * [Introduction to Arrays](https://www.geeksforgeeks.org/introduction-to-arrays/) 2 | * [CMU Slides](https://www.cs.cmu.edu/~15122/handouts/03-arrays.pdf) 3 | * [MIT 1](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-096-introduction-to-c-january-iap-2011/lecture-notes/MIT6_096IAP11_lec04.pdf) 4 | * [CS DOJO Array](https://www.youtube.com/watch?v=pmN9ExDf3yQ) 5 | * [The coding Train Array](https://www.youtube.com/watch?v=NptnmWvkbTw) 6 | * [IDeserve_Array](https://www.youtube.com/watch?v=a7D77DdhlFc&list=PLamzFoFxwoNjw4EpaVZzP-8lqWA9hOmnD) 7 | * [Byte By Byte](https://www.youtube.com/watch?v=HGgdcKbC5ro&list=PLNmW52ef0uwvmnS0UQU4Qf3NvsEREGWoK) 8 | 9 | ### Array Topicwise 10 | 11 | * [Introduction & Advantages](https://www.geeksforgeeks.org/introduction-to-arrays/) 12 | * [Types of Array](#) 13 | * [Fixed Size Array](https://chortle.ccsu.edu/java5/Notes/chap54/ch54_2.html) 14 | * [Dynamic Size Array](https://www.interviewcake.com/concept/java/dynamic-array) 15 | * [Operations On Array](https://github.com/skjha1/Data-Structure-Algorithm-Programs/blob/master/Array/Problems/39%20Practice%20whole%20array.cpp) 16 | * [Problems](https://github.com/skjha1/Data-Structure-Algorithm-Programs/tree/master/Array/Problems) 17 | * [Vector](https://www.geeksforgeeks.org/vector-in-cpp-stl/) 18 | * [map](https://www.geeksforgeeks.org/map-associative-containers-the-c-standard-template-library-stl/) 19 | * [set](https://www.geeksforgeeks.org/set-in-cpp-stl/) 20 | -------------------------------------------------------------------------------- /Array/Syllabus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skjha1/Data-Structure-Algorithm-Programs/e300292abb0e1313dac107e02bc3b7b5a22c11a3/Array/Syllabus.pdf -------------------------------------------------------------------------------- /Graph/Basic/05 DepthFirstSearchUsingRecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void DFS(int u, int A[][8], int n){ 6 | static int visited[8] {0}; 7 | 8 | if (visited[u] == 0){ 9 | cout << u << ", " << flush; 10 | visited[u] = 1; 11 | for (int v=1; v " << flush; 31 | DFS(4, A, 8); 32 | cout << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Graph/Basic/07 Breadth First Search Using STLQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void BFS(int vtx, int A[][8], int n){ 7 | queue Q; 8 | int visited[8] {0}; 9 | 10 | // Initial 11 | cout << vtx << ", " << flush; // Visit vertex 12 | visited[vtx] = 1; 13 | Q.emplace(vtx); 14 | 15 | // Explore 16 | while (!Q.empty()){ 17 | int u = Q.front(); // Vertex u for exploring 18 | Q.pop(); 19 | for (int v=1; v<=n; v++){ // Adjacent vertices of vertex u 20 | if (A[u][v] == 1 && visited[v] == 0){ // Adjacent vertex and not visited 21 | cout << v << ", " << flush; // Visit vertex 22 | visited[v] = 1; 23 | Q.emplace(v); 24 | } 25 | } 26 | } 27 | cout << endl; 28 | } 29 | 30 | int main (){ 31 | 32 | int A[8][8] = {{0, 0, 0, 0, 0, 0, 0, 0}, 33 | {0, 0, 1, 1, 1, 0, 0, 0}, 34 | {0, 1, 0, 1, 0, 0, 0, 0}, 35 | {0, 1, 1, 0, 1, 1, 0, 0}, 36 | {0, 1, 0, 1, 0, 1, 0, 0}, 37 | {0, 0, 0, 1, 1, 0, 1, 1}, 38 | {0, 0, 0, 0, 0, 1, 0, 0}, 39 | {0, 0, 0, 0, 0, 1, 0, 0}}; 40 | 41 | cout << "Vertex: 1 -> " << flush; 42 | BFS(1, A, 8); 43 | 44 | cout << "Vertex: 4 -> " << flush; 45 | BFS(4, A, 8); 46 | 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Heap/03 Insert in Heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void Insert(int A[], int n) // it is taking a heap array as well as index of element thst you want to insert 3 | { 4 | int i = n, temp; // i sud point on index of element when it is inserted i.e i will start from n // take a temp variable 5 | temp = A[i]; // temp variable sud have value which we want to strore in heap 6 | // now compare and swap the element or arrage it 7 | while (i > 1 && temp > A[i / 2]) // as long as i is greater then 1 and temp is grateter then parent // if temp is greater then parent copy the patend value 8 | { 9 | A[i] = A[i / 2]; // a of i copy the value of a of i by 2 10 | i = i / 2; // and i sud be moved to the parent 11 | } 12 | A[i] = temp; // finally we move to last place //temp reach at the right place // then copy the element a of i as temp 13 | } 14 | 15 | int main() { 16 | int H[] = { 0,10,20,30,25,5,40,35 }; // 1st pos will be 0 since it starts from 1 17 | int i; 18 | for (i = 2; i <= 7; i++) // using loop we are inserting elements // 1st is inserted so we will start from 2 19 | Insert(H, i); // every time set element to the index 20 | 21 | 22 | for (i = 1; i <= 7; i++) // using for loop to Display all the elements 23 | printf("%d ", H[i]); // print all the elements as a heap 24 | printf("\n"); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /LinkedList/Basic Consept/03 Count and sum of LL.cpp: -------------------------------------------------------------------------------- 1 | //Count and Sum Linked List 2 | #include 3 | #include 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | }*first=NULL; 9 | void create(int A[],int n) 10 | { 11 | int i; 12 | struct Node *t,*last; 13 | first=(struct Node *)malloc(sizeof(struct Node)); 14 | first->data=A[0]; 15 | first->next=NULL; 16 | last=first; 17 | 18 | for(i=1;idata=A[i]; 22 | t->next=NULL; 23 | last->next=t; 24 | last=t; 25 | } 26 | } 27 | int count(struct Node *p) 28 | { 29 | int l=0; 30 | while(p) 31 | { 32 | l++; 33 | p=p->next; 34 | } 35 | return l; 36 | } 37 | int Rcount(struct Node *p) 38 | { 39 | if(p!=NULL) 40 | return Rcount(p->next)+1; 41 | else 42 | return 0; 43 | } 44 | int sum(struct Node *p) 45 | { 46 | int s=0; 47 | 48 | while(p!=NULL) 49 | { 50 | s+=p->data; 51 | p=p->next; 52 | } 53 | return s; 54 | } 55 | int Rsum(struct Node *p) 56 | { 57 | if(p==NULL) 58 | return 0; 59 | else 60 | return Rsum(p->next)+p->data; 61 | } 62 | int main() 63 | { 64 | int A[]={3,5,7,10,25,8,32,2}; 65 | create(A,8); 66 | 67 | printf("Count %d\n",count(first)); 68 | printf("Sum %d\n",sum(first)); 69 | 70 | 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /LinkedList/Basic Consept/04 Printing Max element.c: -------------------------------------------------------------------------------- 1 | //Max element from Linked List 2 | #include 3 | #include 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | }*first=NULL; 9 | void create(int A[],int n) 10 | { 11 | int i; 12 | struct Node *t,*last; 13 | first=(struct Node *)malloc(sizeof(struct Node)); 14 | first->data=A[0]; 15 | first->next=NULL; 16 | last=first; 17 | 18 | for(i=1;idata=A[i]; 22 | t->next=NULL; 23 | last->next=t; 24 | last=t; 25 | } 26 | } 27 | int Max(struct Node *p) 28 | { 29 | int max=INT_MIN; 30 | 31 | while(p) 32 | { 33 | if(p->data>max) 34 | max=p->data; 35 | p=p->next; 36 | } 37 | return max; 38 | 39 | } 40 | int RMax(struct Node *p) 41 | { 42 | int x=0; 43 | 44 | if(p==0) 45 | return INT_MIN; 46 | x=RMax(p->next); 47 | if(x>p->data) 48 | return x; 49 | else 50 | return p->data; 51 | } 52 | int main() 53 | { 54 | int A[]={3,5,7,10,25,8,32,2}; 55 | create(A,8); 56 | 57 | printf("Max %d\n",Max(first)); 58 | 59 | 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /LinkedList/Basic Consept/06 Searching Element .cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | int data; 6 | struct Node* next; 7 | }*first =NULL; 8 | void create(int a[], int n) 9 | { 10 | int i; 11 | struct Node * t, * last; 12 | first = (struct Node*)malloc(sizeof(struct Node)); 13 | first->data = a[0]; 14 | first->data = NULL; 15 | last = first; 16 | for (i = 1; i < n; i++) 17 | { 18 | t = (struct Node*)malloc(sizeof(struct Node)); 19 | t->data = a[i]; 20 | t->next = NULL; 21 | last->next = t; 22 | last = t; 23 | 24 | } 25 | 26 | } 27 | struct Node* Lsearch(struct Node* p, int key) 28 | { 29 | while (p != NULL) 30 | { 31 | if (key == p->data) 32 | return p; 33 | p = p->next; 34 | } 35 | return NULL; 36 | } 37 | int main() 38 | { 39 | struct Node* temp; 40 | int a[] = { 1,2,3,4,5,8,9,7 }; 41 | create(a,9); 42 | temp = Lsearch(first, 9); 43 | if (temp) 44 | { 45 | printf("Key is found %d", temp->data); 46 | 47 | } 48 | else 49 | printf("Key not found !"); 50 | return 0; 51 | 52 | } -------------------------------------------------------------------------------- /LinkedList/Basic Consept/10 Insert in sorted ll.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | int data; 6 | struct Node* next; 7 | }*first=NULL; 8 | void Create(int A[], int n) 9 | { 10 | int i; 11 | struct Node* last, * t; 12 | first = (struct Node*)malloc(sizeof(struct Node)); 13 | first->data = A[0]; 14 | first->next = NULL; 15 | last = first; 16 | 17 | for (i = 1; i < n; i++) 18 | { 19 | t = (struct Node*)malloc(sizeof(struct Node)); 20 | t->data = A[i]; 21 | t->next = NULL; 22 | last->next = t; 23 | last = t; 24 | } 25 | } 26 | void Display(struct Node* p) 27 | { 28 | while (p != NULL) 29 | { 30 | printf("->%d", p->data); 31 | p = p->next; 32 | } 33 | } 34 | void Sortedinsert(struct Node* p, int x) 35 | { 36 | struct Node* t, * q = NULL; 37 | t = (struct Node*)malloc(sizeof(struct Node)); 38 | t->data = x;/* first node is ready*/ 39 | t->next = NULL; 40 | if (first == NULL) 41 | { 42 | first = t; 43 | } 44 | else 45 | { 46 | while (p && p->data < x) 47 | { 48 | q = p; 49 | p = p->next; 50 | } 51 | if (p == first) 52 | { 53 | t->next = first; 54 | first = t; 55 | } 56 | else { 57 | t->next = q->next; 58 | q->next = t; 59 | } 60 | } 61 | } 62 | 63 | int main () 64 | { 65 | int a[]= {10,20,30,40,50}; 66 | Create (a,sizeof (a)/sizeof(int)); 67 | Sortedinsert (first,35); 68 | Display(first); 69 | printf("\n"); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /LinkedList/Basic Consept/16 Reversing using Recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | int data; 6 | struct Node* next; 7 | }*first=NULL; 8 | void Create(int A[], int n) 9 | { 10 | int i; 11 | struct Node* last, * t; 12 | first = (struct Node*)malloc(sizeof(struct Node)); 13 | first->data = A[0]; 14 | first->next = NULL; 15 | last = first; 16 | 17 | for (i = 1; i < n; i++) 18 | { 19 | t = (struct Node*)malloc(sizeof(struct Node)); 20 | t->data = A[i]; 21 | t->next = NULL; 22 | last->next = t; 23 | last = t; 24 | } 25 | } 26 | void Display(struct Node* p) 27 | { 28 | while (p != NULL) 29 | { 30 | printf("->%d", p->data); 31 | p = p->next; 32 | } 33 | } 34 | void RDisplay(struct Node* p) 35 | { 36 | if (p!=NULL) 37 | { 38 | printf("->%d", p->data); 39 | RDisplay(p->next); 40 | } 41 | } 42 | 43 | 44 | //reversing using Recursion 45 | void Rreverse(struct Node* q, struct Node* p) // taking two pointer p and q ; p will always ahead and q will follow that; 46 | { 47 | if (p != NULL) 48 | { 49 | Rreverse(p, p->next); // passing two pointer 50 | p->next = q;// while recturnig it will make p next as q 51 | } 52 | else 53 | first = q; // while p i null make first as q ; 54 | } 55 | 56 | int main() 57 | { 58 | int A[] = { 10,20,30,40,50 }; 59 | Create(A, sizeof (A)/sizeof (int )); 60 | Rreverse(NULL,first); 61 | Display(first); 62 | printf("\n\n"); 63 | 64 | return 0; 65 | 66 | } 67 | -------------------------------------------------------------------------------- /LinkedList/Basic Consept/display ll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | int main() { 11 | 12 | int A[] = { 3, 5, 7, 10, 15 }; 13 | 14 | Node* head = new Node; 15 | 16 | Node* temp; 17 | Node* last; 18 | 19 | head->data = A[0]; 20 | head->next = nullptr; 21 | last = head; 22 | 23 | // Create a Linked List 24 | for (int i = 1; i < sizeof(A) / sizeof(A[0]); i++) { 25 | 26 | // Create a temporary Node 27 | temp = new Node; 28 | 29 | // Populate temporary Node 30 | temp->data = A[i]; 31 | temp->next = nullptr; 32 | 33 | // last's next is pointing to temp 34 | last->next = temp; 35 | last = temp; 36 | } 37 | 38 | // Display Linked List 39 | Node* p = head; 40 | 41 | while (p != nullptr) { 42 | cout << p->data << " -> " << flush; 43 | p = p->next; 44 | } 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Matrix/01 Diagonal matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct matrix { 3 | int A[10]; 4 | int n; 5 | };// it is going to modify the matrix so it shoud be call by address so ptr is taken 6 | void set (struct matrix *m ,int i , int j, int x) // i,j is for index at which you want to set elements, xis what is the element you want to insert 7 | { 8 | if (i==j) // as we are going to take only non zero elements so index sud be equal 9 | m->A[i-1]=x; // store elemnts at diag 10 | } 11 | int get(struct matrix m ,int i, int j) 12 | { 13 | if (i==j) 14 | return m.A[i-1]; 15 | else 16 | return 0; 17 | } 18 | void Display (struct matrix m) 19 | { 20 | int i,j; 21 | for (i=0;i 2 | using namespace std; 3 | class Diagonal // Difining a diagonal class 4 | { 5 | private: // passing datamembers with private 6 | int n; // Dimension 7 | int *A; // array as pointer 8 | public: // inside public the very 1st thing is constructor 9 | Diagonal() // non parametrized constructor 10 | { 11 | n=2; 12 | A= new int[2]; 13 | } 14 | Diagonal (int n) // parametarized constructor 15 | { 16 | this->n=n; 17 | A=new int [n]; 18 | } 19 | ~Diagonal () // since we are creating memory in heap so for realissing memory we will use destructor 20 | { 21 | delete []A; 22 | } 23 | void Set (int i, int j, int x); 24 | int Get (int i, int j); 25 | void Display(); 26 | }; 27 | void Diagonal::Set(int i,int j,int x) 28 | { 29 | if (i==j) 30 | A[i-1]=x; 31 | } 32 | int Diagonal::Get(int i,int j) 33 | { 34 | if (i==j) 35 | { 36 | return A[i-1]; 37 | 38 | } 39 | else return 0; 40 | } 41 | void Diagonal::Display() 42 | 43 | { 44 | for (int i=0;i 2 | #include 3 | struct Matrix 4 | { 5 | int *A; 6 | int n; 7 | }; 8 | void Set(struct Matrix *m,int i,int j,int x) 9 | { 10 | if(i>=j) 11 | m->A[m->n*(j-1)+(j-2)*(j-1)/2+i-j]=x; // using column major formula 12 | } 13 | int Get(struct Matrix m,int i,int j) 14 | { 15 | if(i>=j) 16 | return m.A[m.n*(j-1)+(j-2)*(j-1)/2+i-j]; 17 | else 18 | return 0; 19 | } 20 | void Display(struct Matrix m) 21 | { 22 | int i,j; 23 | for(i=1;i<=m.n;i++) 24 | { 25 | for(j=1;j<=m.n;j++) 26 | { 27 | if(i>=j) 28 | printf("%d ",m.A[m.n*(j-1)+ 29 | (j-2)*(j-1)/2+i-j]); 30 | else 31 | printf("0 "); 32 | } 33 | printf("\n"); 34 | } 35 | } 36 | int main() 37 | { 38 | struct Matrix m; 39 | int i,j,x; 40 | 41 | printf("Enter Dimension\n"); 42 | scanf("%d",&m.n); 43 | m.A=(int *)malloc(m.n*(m.n+1)/2*sizeof(int)); 44 | printf("enter all elements"); 45 | for(i=1;i<=m.n;i++) 46 | { 47 | for(j=1;j<=m.n;j++) 48 | { 49 | scanf("%d",&x); 50 | Set(&m,i,j,x); 51 | } 52 | } 53 | printf("\n\n"); 54 | Display(m); 55 | 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Problems/Memory Allocation/01 create new node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Your job is to create number of nodes as per user request using pointer 3 | 4 4 | 1 4 5 99 5 | 1 4 5 99 6 | 5 7 | 1 4 5 99 100 8 | 1 4 5 99 100 9 | 2 10 | 1 4 11 | 1 4 12 | 13 | */ 14 | #include 15 | #include 16 | int main () 17 | { 18 | int *p,i,no; 19 | scanf("%d",&no); 20 | p = (int *)malloc (no*sizeof(int)); 21 | for (i=0;i 18 | #include 19 | int main () 20 | { 21 | int i,no,*p,sum=0; 22 | scanf("%d",&no); 23 | p= (int * ) malloc (no*sizeof(int)); 24 | 25 | for (i=0;i<=no;i++) 26 | { 27 | scanf("%d",p+i); 28 | } 29 | for(i=0;i 25 | using namespace std; 26 | int main () 27 | { 28 | int *a,*b,temp,x,y; 29 | scanf("%d%d",&x,&y); 30 | printf("x=%d\ny=%d\n",x,y); 31 | 32 | a=&x;// a assign to x 33 | b=&y;// b assign to y 34 | 35 | temp=*a; 36 | *a=*b; 37 | *b=temp; 38 | printf("After Swapping\n"); 39 | printf("x=%d\ny=%d\n",x,y); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Problems/Memory Allocation/05 Finding Square Roots.c: -------------------------------------------------------------------------------- 1 | /* 2 | In olden days finding square roots seemed to be difficult but nowadays it can be easily done using in-built functions available across many languages . 3 | 4 | Assume that you happen to hear the above words and you want to give a try in finding the square root of any given integer using in-built functions. So here's your chance. 5 | Input 6 | 7 | The first line of the input contains an integer T, the number of test cases. T lines follow. Each T contains an integer N whose square root needs to be computed. 8 | Output 9 | 10 | For each line of input output the square root of the input integer. 11 | Constraints 12 | 13 | 1<=T<=20 14 | 1<=N<=10000 15 | TEST CASE 1 16 | 17 | INPUT 18 | 3 19 | 10 20 | 5 21 | 10000 22 | OUTPUT 23 | 3 24 | 2 25 | 100 26 | TEST CASE 2 27 | 28 | INPUT 29 | 1 30 | 10 31 | OUTPUT 32 | 3 33 | */ 34 | 35 | #include 36 | #include 37 | #include 38 | int main() { 39 | int no,*p,i,a; 40 | scanf("%d",&no); 41 | p=(int *)malloc(no*sizeof(int)); 42 | for (i=0;i 2 | using namespace std; 3 | int fun1(int n) // recursive fxn 4 | { 5 | if (n > 0) // the base condition it is used for terminating the recursion // until this condition satisfy fxn will all itself 6 | { 7 | cout << n << " "; // printing the value 8 | fun1(n - 1); // recursion will have two phase calling and returning // this is calling phase 9 | } // these all will utilise stack memory total (n+1) activation record will be created // recursion fxn is memory consuming fxn 10 | return 0; 11 | } 12 | 13 | int main() 14 | { 15 | int a; 16 | cin >> a; 17 | fun1(a); // function is called now the control to this fxn to complete the task 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Recursion/02 head recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void func1(int n) 3 | { 4 | if (n>0) // Base condition 5 | { 6 | func1(n-1); // 1st call then print 7 | printf("%d ",n); 8 | } 9 | } 10 | int main (){ 11 | int x=9; 12 | func1(x); // passing parameter to fxn 13 | return 0; 14 | } 15 | 16 | // This will give output as 1 2 3 4 5 6 7 8 9 17 | 18 | 19 | #include 20 | using namespace std; 21 | 22 | void func(int n) 23 | { 24 | if (n>0) 25 | { 26 | func(n-1); 27 | cout<< n<<" "; 28 | } 29 | } 30 | 31 | 32 | 33 | int main () 34 | { 35 | int a; 36 | cin>>a; 37 | func(a); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Recursion/03 global variable recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | int x=0; 3 | int func (int n) 4 | 5 | { 6 | 7 | if (n>0) 8 | { 9 | x++; 10 | return func(n-1)+x; 11 | } 12 | return 0; 13 | } 14 | int main () 15 | { 16 | int a=5; 17 | a= func(5); 18 | printf("%d\n",a); 19 | 20 | a=func(5); 21 | printf("%d\n",a); 22 | } 23 | -------------------------------------------------------------------------------- /Recursion/04 static variable in reciursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | //int x = 0; // this is globally accessed in all fxn 3 | int func(int n) 4 | { 5 | static int x=0; // this will take memory in main 6 | if (n > 0) // base condition of recursion 7 | { 8 | x++; // everytime x is getting incremented 9 | return func(n - 1) + x; // at returning time the addition of x will be performed // it will add last increatemented value everytinme 10 | } 11 | return 0; 12 | } 13 | int main() 14 | { 15 | int a,b; 16 | scanf_s("%d",&a); 17 | printf("%d\n",func(a)); // fxn call 18 | b = func(a); // at 2nd time the value of x will be equal to the last incremented value if 5 is first time then this will be updated as 10 19 | printf("%d", b); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Recursion/05 Tree recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fun(int n) 3 | 4 | { 5 | if (n>0) 6 | { 7 | printf("%d ",n); 8 | fun(n-1); 9 | fun(n-1); 10 | 11 | } 12 | return 0; 13 | } 14 | int main () 15 | { 16 | fun(4); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Recursion/06 nested recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fun(int n) 3 | { 4 | if (n>100) 5 | return n-10; 6 | else 7 | return fun(fun(n+11)); 8 | } 9 | int main () 10 | { 11 | int r; 12 | r=fun(800); // for any value more than 100 it will print ( value - 10 ) 13 | printf("%d\n",r); // for any value less then 100 or equal to 100 it will return 91 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Recursion/07 Sum of n number using recursion.c: -------------------------------------------------------------------------------- 1 | /* 2 | Q- The Sum of 1st n terms 3 | 4 | 5 | Code by Shivendra K JHa 6 | */ 7 | #include 8 | int sum (int n) 9 | { 10 | if (n>0) 11 | return sum(n-1)+n; 12 | else 13 | return 0; 14 | } 15 | int main () 16 | { 17 | int a; 18 | printf("Enter a number "); 19 | scanf("%d",&a); 20 | printf("The sum upto that number : %d",sum(a)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Recursion/08 Factorial of a numbet using recursion.c: -------------------------------------------------------------------------------- 1 | /* Factorial of a numbet using recursion 2 | 3 | code by shivendra k jha 4 | 5 | */ 6 | 7 | # include 8 | int fact(int n) 9 | { 10 | if (n==0) 11 | return 1; 12 | else 13 | return fact(n-1)*n; 14 | } 15 | int main () 16 | { 17 | int x; 18 | printf("Enter a number "); 19 | scanf("%d",&x); 20 | int a=fact(x); 21 | printf("The factorail of given number is %d : ",a); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Recursion/09 Sum Of given number using loop.c: -------------------------------------------------------------------------------- 1 | /* Sum Of given number using loop 2 | 3 | code by shivendra k jha 4 | 5 | */ 6 | 7 | int sum(int n) 8 | { 9 | if(n==0) 10 | return 0; 11 | return sum(n-1)+n; 12 | } 13 | int Isum(int n) 14 | { 15 | int s=0,i; 16 | for(i=1;i<=n;i++) 17 | s=s+i; 18 | 19 | return s; 20 | } 21 | int main() 22 | { 23 | int r=sum(5); 24 | printf("%d ",r); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Recursion/10 powe using recursion.cpp: -------------------------------------------------------------------------------- 1 | /* Power Using Recursion 2 | 3 | code by shivendra k jha 4 | 5 | */ 6 | 7 | #include 8 | #include 9 | int power(m,n) 10 | { 11 | if (m==0) 12 | return 1; 13 | else 14 | return pow(m,n-1)*m; 15 | 16 | } 17 | int main () 18 | { 19 | int a,b; 20 | printf("enter the numbers "); 21 | scanf("%d %d",&a,&b); 22 | int c=power(a,b); 23 | printf("%d",c); 24 | return 0; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Recursion/11 power function.c: -------------------------------------------------------------------------------- 1 | /* Power Using Recursion 2 | 3 | code by shivendra k jha 4 | 5 | */ 6 | 7 | #include 8 | #include 9 | int power(int m,int n) 10 | { 11 | if (n==0) 12 | return 1; 13 | if (n%2==0) 14 | return pow(m*m,n/2); 15 | else 16 | return m*pow(m*m,(n-1)/2); 17 | 18 | } 19 | int main () 20 | { 21 | int a,b; 22 | printf("enter the numbers "); 23 | scanf("%d %d",&a,&b); 24 | int c=power(a,b); 25 | printf("%d",c); 26 | return 0; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Recursion/12 Taylor Series using Static variables.cpp: -------------------------------------------------------------------------------- 1 | /* Power Using Recursion 2 | 3 | code by shivendra k jha 4 | 5 | */ 6 | 7 | #include 8 | int e(int x, int n) 9 | { 10 | static double p=1,f=1; 11 | int r; 12 | if (n==0) 13 | return 1; 14 | else 15 | { 16 | r=e(x,n-1); 17 | p=p*x; 18 | f=f*n; 19 | return r+p/f; 20 | } 21 | 22 | } 23 | int main () 24 | { 25 | int a,b; 26 | printf("Enter the numbers to be operated "); 27 | scanf("%d%d",&a,&b); 28 | int c=e(a,b); 29 | printf("taylor seris of e pow x is %d ",c); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Recursion/13 Taylor Serie honers rule.c: -------------------------------------------------------------------------------- 1 | #include 2 | double e(int x, int n) 3 | { 4 | static double s; 5 | if(n==0) 6 | return s; 7 | s=1+x*s/n; 8 | return e(x,n-1); 9 | 10 | } 11 | int main() 12 | { 13 | printf("%lf \n",e(2,10)); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Recursion/14 Taylor Serie Iterative.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | double e(int x, int n) 3 | { 4 | double s=1; 5 | int i; 6 | double num=1; 7 | double den=1; 8 | 9 | for(i=1;i<=n;i++) 10 | { 11 | num*=x; 12 | den*=i; 13 | s+=num/den; 14 | } 15 | return s; 16 | } 17 | int main() 18 | { 19 | printf("%lf \n",e(1,10)); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Recursion/15 Indirect Recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | int funB(int n); 3 | int funA(int n) 4 | { 5 | if (n>0) 6 | { 7 | printf("%d ",n); 8 | funB(n-1); 9 | } 10 | return 0; 11 | } 12 | int funB (int n) 13 | { 14 | if (n>1) 15 | { 16 | printf("%d ",n); 17 | funA(n/2); 18 | } 19 | return 0; 20 | } 21 | int main () 22 | { 23 | funA(20); 24 | return 0; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Recursion/16 Fibbonaci series using loop and recursion .c: -------------------------------------------------------------------------------- 1 | #include 2 | int fib(int n) 3 | { 4 | int t0=0,t1=1,sum=0,i; 5 | if (n<=1) return n; 6 | for (i=2;i<=n;i++) 7 | { 8 | sum=t0+t1; 9 | t0=t1; 10 | t1=sum; 11 | } 12 | return sum; 13 | } 14 | 15 | int rfib(int n ) 16 | { 17 | if (n<=1) 18 | return n; 19 | else 20 | return rfib(n-2)+rfib(n-1); 21 | } 22 | int main () 23 | { 24 | int a ; 25 | printf("Enter a number "); 26 | scanf("%d",&a); 27 | printf("fibbo using recursive function %d \n",rfib(a)); // time complexity for this is 2 pow n 28 | printf("fibbo using loop is %d \n",fib(a)); // time complexity for this is o(n) 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Recursion/17 fibbonacci using memoization.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fib(int n) 3 | { 4 | int t0=0,t1=1,sum=0,i; 5 | if (n<=1) return n; 6 | for (i=2;i<=n;i++) 7 | { 8 | sum=t0+t1; 9 | t0=t1; 10 | t1=sum; 11 | } 12 | return sum; 13 | } 14 | 15 | int rfib(int n ) 16 | { 17 | if (n<=1) 18 | return n; 19 | else 20 | return rfib(n-2)+rfib(n-1); 21 | } 22 | int F[10]; 23 | int mfib(int n) 24 | { 25 | if (n<=1) 26 | { 27 | F[n]=n; // in function F of n simply return the value of the n if n is less then 1; 28 | return n; 29 | } 30 | else 31 | { 32 | if (F[n-2]==-1) // if place is not called then 33 | F[n-2]=mfib(n-2); // call recursively 34 | if (F[n-1]==-1); // if this is also not called then 35 | F[n-1]=mfib(n-1);// call recursively 36 | 37 | return F[n-2]+F[n-1]; // at last return the sum of the called terms 38 | } 39 | 40 | } 41 | 42 | int main () 43 | { 44 | int a,i ; 45 | for (i=0;i<=10;i++) 46 | F[i]=F[i]-1; 47 | printf("Enter a number "); 48 | scanf("%d",&a); 49 | printf("fibbo using recursive function %d \n",rfib(a)); // time complexity for this is 2 pow n 50 | printf("fibbo using loop is %d \n",fib(a)); // time complexity for this is o(n) 51 | printf("Fibbo using memoization is %d ",mfib(a)); // Time complexity using memoization is o(n) where space is takes very less , it works on dynamic programmin 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Recursion/18 nCr formula.c: -------------------------------------------------------------------------------- 1 | /* Combination nCr problem 2 | 3 | code By Shivendra jha 4 | 5 | 6 | */ 7 | 8 | #include 9 | int fact(int n) 10 | { 11 | if (n<=1) 12 | return 1; 13 | else 14 | return fact(n-1)*n; 15 | } 16 | int nCr(int n,int r) 17 | 18 | 19 | { 20 | int num,den; 21 | num= fact(n); 22 | den=fact(r)*fact(n-r); 23 | 24 | return num/den; 25 | } 26 | int main () 27 | { 28 | int x,y; 29 | printf("Enter the value of n and r \n"); 30 | scanf("%d%d",&x,&y); 31 | printf("The nCr is : %d",nCr(x,y)); 32 | } 33 | -------------------------------------------------------------------------------- /Recursion/19 Tower of Hanoi .c: -------------------------------------------------------------------------------- 1 | #include 2 | int TOH (int n,int A, int B , int C) 3 | { 4 | if (n>0) 5 | { 6 | TOH (n-1,A,C,B); 7 | printf("(%d,%d) \n",A,C); // This gives the total no of steps we get 8 | TOH(n-1,B,A,C); 9 | } 10 | return 0; 11 | } 12 | int main() 13 | { 14 | int p,a,b,c; 15 | printf("Enter the no of disk and the number of tower "); 16 | scanf("%d %d %d %d",&p,&a,&b,&c); // Here p is no of disk & a,b,c are no of tower 17 | printf("finaally we get all the disk at tower 1 is %d \n",TOH(p,a,b,c)); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Recursion/ex.c: -------------------------------------------------------------------------------- 1 | #include 2 | void tail(int n) 3 | { 4 | if (n > 0) 5 | { 6 | printf("%d ", n); 7 | tail(n-1); 8 | } 9 | } 10 | int main() 11 | { 12 | int a; 13 | scanf_s("%d", &a); 14 | tail(a); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Recursion/ex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void tail(int n) 3 | { 4 | if (n > 0) 5 | { 6 | printf("%d ", n); 7 | tail(n-1); 8 | } 9 | } 10 | int main() 11 | { 12 | int a; 13 | scanf_s("%d", &a); 14 | tail(a); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Sorting Technique/01 Bubble Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int* x, int* y) // function for swapping 5 | { 6 | int temp = *x; 7 | *x = *y; 8 | *y = temp; 9 | } 10 | 11 | void Bubble(int A[], int n) // size of array // no of elements 12 | { 13 | int i, j, flag = 0; // flag is for adptive (Criteria) 14 | for (i = 0; i < n - 1; i++) 15 | { 16 | flag = 0; 17 | for (j = 0; j < n - i - 1; j++) 18 | { 19 | if (A[j] > A[j + 1]) 20 | { 21 | swap(&A[j], &A[j + 1]); // if is greater then swap the elements 22 | flag = 1; 23 | } 24 | } 25 | if (flag == 0) 26 | break; 27 | 28 | } 29 | } 30 | int main() 31 | { 32 | int A[20], i; 33 | int n = sizeof(A) / sizeof(int); 34 | scanf_s("%d", &n); 35 | for (i = 0; i < n; i++) 36 | { 37 | scanf_s("%d", & A[i]); 38 | } 39 | 40 | Bubble(A, n); // callig the funtion 41 | 42 | for (i = 0; i < n; i++) 43 | printf("%d ", A[i]); 44 | printf("\n"); 45 | 46 | return 0; 47 | } 48 | 49 | // Bubble sort is Adptive: if the list is sorted it will use flag // Bubble sort is stable : in case of duplicate 50 | // time complexity is : min = o(n) - in the case of Adaptivity 51 | // : max = o(n2) -------------------------------------------------------------------------------- /Sorting Technique/02 BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void Print(T& vec, int n, string s){ 7 | cout << s << ": [" << flush; 8 | for (int i=0; i A[j+1]){ 28 | swap(&A[j], &A[j+1]); 29 | flag = 1; 30 | } 31 | } 32 | if (flag == 0){ 33 | return; 34 | } 35 | } 36 | } 37 | 38 | int main() { 39 | 40 | int A[] = {3, 7, 9, 10, 6, 5, 12, 4, 11, 2}; 41 | int n = sizeof(A)/sizeof(A[0]); 42 | Print(A, n, "\t\tA"); 43 | 44 | BubbleSort(A, n); 45 | Print(A, n, " Sorted A"); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Sorting Technique/03 Insertionsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int* a, int* b) 3 | { 4 | int temp = *a; 5 | *a = *b; 6 | *b = temp; 7 | } 8 | void Insertionsort(int A[], int n) 9 | { 10 | int i, j, x; 11 | for (i = 1; i < n; i++) // this is for repetation of passes 12 | { 13 | j = i - 1; // set j as one before the last 14 | x = A[i]; // the elemets that we have to insert 15 | // go on shifting the elements as long as a[j] is greater then x and everytime reduce j by 1 ; j-- 16 | while (j > -1 && A[j] > x) // shifting the elements 17 | { 18 | // when place is found to insert then 19 | A[j + 1] = A[j]; // everytime at the next location copy the elemets of j 20 | j--; // decrement j 21 | } 22 | A[j + 1] = x; // at end copy the element at appropriate position 23 | } 24 | 25 | } 26 | int main() 27 | { 28 | int A[20],no,i; 29 | scanf_s("%d", &no); 30 | for (i = 0; i < no; i++) 31 | { 32 | scanf_s("%d", &A[i]); 33 | } 34 | Insertionsort(A, no); 35 | for (i = 0; i < no; i++) 36 | printf("%d ", A[i]); 37 | printf("\n"); 38 | return 0; 39 | 40 | } -------------------------------------------------------------------------------- /Sorting Technique/04 InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void Print(T& vec, int n, string s){ 7 | cout << s << ": [" << flush; 8 | for (int i=0; i-1 && A[j] > x){ 22 | A[j+1] = A[j]; 23 | j--; 24 | } 25 | A[j+1] = x; 26 | } 27 | } 28 | 29 | int main() { 30 | 31 | int A[] = {19, 17, 15, 13, 11, 9, 7, 5, 3, 1}; 32 | Print(A, sizeof(A)/sizeof(A[0]), " A"); 33 | 34 | InsertionSort(A, sizeof(A)/sizeof(A[0])); 35 | Print(A, sizeof(A)/sizeof(A[0]), "Sorted A"); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Sorting Technique/05 Selection Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int* a, int* b) 4 | { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | void selectionsort(int A[], int n) 10 | { 11 | int i, j, k; // for indexes 12 | for (i = 0; i < n - 1; i++) // for passes // in 1st pass we will get the smallest element 13 | { 14 | for (j = k = i; j < n; j++) // for checing all elements // keep i,j,k on 1st pos and strating moving j everytime and compare with k 15 | { 16 | // move j to next element and comp with k . if j is smaller then k then bring k at j.. 17 | if (A[j] < A[k]) // if j is less then k then move k and we will 18 | k = j; // now k is pointing on the smallest elements 19 | } 20 | swap(&A[i], &A[k]); // now swap k with i then we will get the smallest elements at that position (i) // in this only one swap will happen 21 | } 22 | } 23 | int main() 24 | { 25 | int no, i, A[20]; 26 | no = sizeof(A) / sizeof(int); 27 | scanf_s("%d", &no); 28 | for (i = 0; i < no; i++) 29 | { 30 | scanf_s("%d", &A[i]); 31 | } 32 | selectionsort(A, no); 33 | for (i = 0; i < no; i++) 34 | { 35 | printf("%d ", A[i]); 36 | } 37 | printf("\n"); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Sorting Technique/06 SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template 6 | void Print(T& vec, int n, string s){ 7 | cout << s << ": [" << flush; 8 | for (int i=0; i 4 | 5 | void Merge(int A[], int l, int mid, int h) // this is for two array inside one array 6 | { 7 | int i = l, j = mid + 1, k = l; 8 | int B[100]; 9 | 10 | while (i <= mid && j <= h) 11 | { 12 | if (A[i] < A[j]) 13 | B[k++] = A[i++]; // copy element from a to b 14 | else 15 | B[k++] = A[j++]; // copy element from another side of a to b 16 | } 17 | for (; i <= mid; i++) 18 | B[k++] = A[i]; 19 | for (; j <= h; j++) 20 | B[k++] = A[j]; 21 | 22 | for (i = l; i <= h; i++) // this is for copy all the elements from B to A( back ) 23 | A[i] = B[i]; 24 | } 25 | void MergeSort(int A[], int l, int h) 26 | { 27 | int mid; 28 | if (l < h) 29 | { 30 | mid = (l + h) / 2; 31 | MergeSort(A, l, mid); 32 | MergeSort(A, mid + 1, h); 33 | Merge(A, l, mid, h); 34 | 35 | } 36 | } 37 | int main() 38 | { 39 | int A[] = { 11,13,7,12,16,9,24,5,10,3 }, n = 10, i; 40 | 41 | MergeSort(A, 0, 9); 42 | 43 | for (i = 0; i < 10; i++) 44 | printf("%d ", A[i]); 45 | printf("\n"); 46 | 47 | return 0; 48 | } 49 | // time complexity = o(m+n) -------------------------------------------------------------------------------- /Sorting Technique/11 Recursive Mergesort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Merge(int A[], int l, int mid, int h) // this is for two array inside one array 4 | { 5 | int i = l, j = mid + 1, k = l; // it sud be initizlized 6 | int B[100]; 7 | 8 | while (i <= mid && j <= h) 9 | { 10 | if (A[i] < A[j]) 11 | B[k++] = A[i++]; // copy element from a to b 12 | else 13 | B[k++] = A[j++]; // copy element from another side of a to b 14 | } 15 | for (; i <= mid; i++) 16 | B[k++] = A[i]; 17 | for (; j <= h; j++) 18 | B[k++] = A[j]; 19 | 20 | for (i = l; i <= h; i++) // this is for copy all the elements from B to A( back ) 21 | A[i] = B[i]; 22 | } 23 | // recursive fxn for mergesort 24 | void RMergeSort(int A[], int l, int h) 25 | { 26 | int mid; 27 | if (l < h) 28 | { 29 | mid = (l + h) / 2; 30 | RMergeSort(A, l, mid); 31 | RMergeSort(A, mid + 1, h); 32 | Merge(A, l, mid, h); // merging will done in postorder 33 | 34 | } 35 | } 36 | int main() 37 | { 38 | int A[] = { 11,13,7,12,16,9,24,5,10,3 }, n = 10, i; 39 | 40 | RMergeSort(A,0,n-1); //passing array // low // high 41 | 42 | for (i = 0; i < 10; i++) 43 | printf("%d ", A[i]); 44 | printf("\n"); 45 | 46 | return 0; 47 | } 48 | // TC : O(nlogn) // n is time taken for merging and logn is for the logn time the merging will be performed 49 | // this will take extra space -------------------------------------------------------------------------------- /Sorting Technique/17 Shellsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct Array 6 | { 7 | int* A; 8 | int size; 9 | int length; 10 | }; 11 | 12 | void Display(struct Array* arr) 13 | { 14 | int i; 15 | cout << "The elements of the array is " << endl; 16 | for (i = 0; i < arr->length; i++) 17 | cout << arr->A[i] << " "; 18 | } 19 | 20 | void ShellSort(struct Array* A, int n) 21 | { 22 | int gap, i, j, temp; 23 | 24 | for (gap = n / 2; gap >= 1; gap /= 2) 25 | { 26 | for (i = gap; i < n; i++) 27 | { 28 | temp = A->A[i]; 29 | j = i - gap; 30 | while (j >= 0 && A->A[j] > temp) 31 | { 32 | A->A[j + gap] = A->A[j]; 33 | j = j - gap; 34 | } 35 | A->A[j + gap] = temp; 36 | 37 | } 38 | } 39 | 40 | } 41 | 42 | int main() 43 | { 44 | struct Array arr; 45 | cout << "Enter the size of the Array" << endl; 46 | cin >> arr.size; 47 | arr.A = new int[arr.size]; 48 | int no, i; 49 | cout << "Enter the length of the Array " << endl; 50 | cin >> no; 51 | arr.length = 0; 52 | cout << "Enter the elements of the Array" << endl; 53 | for (i = 0; i < no; i++) 54 | cin >> arr.A[i]; 55 | arr.length = no; 56 | ShellSort(&arr,no); 57 | Display(&arr); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Sorting Technique/18 ShellSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | using std::flush; 6 | using std::string; 7 | 8 | template 9 | void Print(T& vec, int n, string s){ 10 | cout << s << ": [" << flush; 11 | for (int i=0; i=1; gap/=2){ 23 | for (int j=gap; j= 0 && A[i] > temp){ 27 | A[i+gap] = A[i]; 28 | i = i-gap; 29 | } 30 | A[i+gap] = temp; 31 | } 32 | } 33 | } 34 | 35 | int main() { 36 | 37 | int A[] = {11, 13, 7, 12, 16, 9, 24, 5, 10, 3}; 38 | int n = sizeof(A)/sizeof(A[0]); 39 | 40 | Print(A, n, "\t\tA"); 41 | ShellSort(A, n); 42 | Print(A, n, " Sorted A"); 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Stack/02 Stack using linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | int data; 6 | struct Node* next; 7 | }*top = NULL; 8 | 9 | void push(int x) 10 | { 11 | struct Node* t; 12 | t = (struct Node*)malloc(sizeof(struct Node)); 13 | if (t == NULL) 14 | printf("Stack is full"); // if heap memory is full then only stack will consider as full 15 | else 16 | { 17 | t->data = x; 18 | t->next = top; 19 | top = t; 20 | } 21 | 22 | } 23 | int pop() 24 | { 25 | struct Node* t; 26 | int x = -1; 27 | if (top == NULL) 28 | { 29 | printf("Stack is Empty"); 30 | } 31 | else 32 | { 33 | t = top; 34 | top = top->next; 35 | x = t->data; 36 | free(t); 37 | } 38 | return x; 39 | } 40 | void Display() 41 | { 42 | struct Node* p; 43 | p = top; 44 | while (p != NULL) 45 | { 46 | printf("%d ",p->data); 47 | p = p->next; 48 | } 49 | printf("\n"); 50 | } 51 | 52 | int main() 53 | { 54 | push(10); 55 | push(20); 56 | push(30); 57 | Display(); 58 | printf("%d", pop()); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Stack/03 Parenthesis checking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | char data; 6 | struct Node* next; 7 | }*top=NULL; 8 | // in c lang we dont have templete so we have to modify data member everywhere ; 9 | 10 | void push(char x) 11 | { 12 | struct Node* t; 13 | t = (struct Node*)malloc(sizeof(struct Node)); 14 | if (t == NULL) 15 | printf("Stack is full 0"); 16 | else 17 | { 18 | t->data = x; 19 | t->next = top; 20 | top = t; 21 | } 22 | 23 | } 24 | int pop() 25 | { 26 | struct Node* t; 27 | int x = -1; 28 | if (top == NULL) 29 | printf("Stack is empty "); 30 | else 31 | { 32 | t = top; 33 | top = top->next; 34 | x = t->data; 35 | free(t); 36 | } 37 | return x; 38 | } 39 | 40 | void Display() 41 | { 42 | struct Node* p; 43 | p = top; 44 | while (p) 45 | { 46 | printf("%d ", p->data); 47 | p = p->next; 48 | } 49 | printf("\n"); 50 | } 51 | 52 | int isBalanced(char* exp) 53 | { 54 | int i; 55 | for (i = 0; exp[i] != '\0'; i++) 56 | { 57 | if (exp[i] == '(') 58 | { 59 | push(exp[i]); 60 | } 61 | else if(exp[i] == ')') 62 | { 63 | if (top == NULL) { 64 | return 0; 65 | } 66 | pop(); 67 | 68 | } 69 | } 70 | if (top == NULL) 71 | return 1; 72 | else 73 | return 0; 74 | } 75 | 76 | 77 | 78 | int main() 79 | { 80 | char* exp = "((a+b)*(c-d))"; 81 | printf("%d", isBalanced(exp)); 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /Stack/08 Nearest greater to left(NGL).cpp: -------------------------------------------------------------------------------- 1 | class 2 | { 3 | public: 4 | vector nextLargerElement(vector arr, int n){ 5 | vector v; // creating a vector for storing result 6 | stack s; // creating a stack for temp. hold the values from array 7 | for (int i=0;i0 && s.top()>arr[i]) // when there is element in stack and stack top is greater then array element 11 | { 12 | v.push_back(s.top()); // take stack top in the result vector 13 | } 14 | else if (s.size()>0 && s.top()<=arr[i]) // when there is element in stack and that element is less then array element 15 | { 16 | while(s.size()>0 && s.top()<=arr[i]) // upto when there is element and stack top is less then array's element delete the element from stack 17 | { 18 | s.pop(); // delete the element from stack 19 | } 20 | if (s.size()==0) // when stack became empty return -1 21 | { 22 | v.push_back(-1); 23 | } 24 | else 25 | { 26 | v.push_back(s.top()); // else push stack top in the vector 27 | } 28 | } 29 | s.push(arr[i]); // push array in stack 30 | } 31 | return v; 32 | } 33 | }; 34 | 35 | 36 | // Time Complexity: O(N) 37 | // Auxiliary Space: O(N) 38 | -------------------------------------------------------------------------------- /Stack/09 Nearest smaller to left.cpp: -------------------------------------------------------------------------------- 1 | class 2 | { 3 | public: 4 | vector nextLargerElement(vector arr, int n){ 5 | vector v; // creating a vector for storing result 6 | stack s; // creating a stack for temp. hold the values from array 7 | for (int i=0;i0 && s.top()0 && s.top()>=arr[i]) // when there is element in stack and that element is greater then equal to array element 15 | { 16 | while(s.size()>0 && s.top()>=arr[i]) // upto when there is element and stack top is greater then equal to array's element delete the element from stack 17 | { 18 | s.pop(); // delete the element from stack 19 | } 20 | if (s.size()==0) // when stack became empty return -1 21 | { 22 | v.push_back(-1); 23 | } 24 | else 25 | { 26 | v.push_back(s.top()); // else push stack top in the vector 27 | } 28 | } 29 | s.push(arr[i]); // push array in stack 30 | } 31 | return v; 32 | } 33 | }; 34 | 35 | 36 | // Time Complexity: O(N) 37 | // Auxiliary Space: O(N) 38 | -------------------------------------------------------------------------------- /String/01 Length of string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main () 4 | { 5 | char A[]="Shivendra"; 6 | int i; 7 | for (i=0;A[i]!='\0';i++) 8 | { 9 | 10 | } 11 | cout<<"Length of string is "< 2 | using namespace std; 3 | int main () 4 | { 5 | char A[]="SHIVENDRA"; 6 | int i; 7 | for (i=0;A[i]!='\0';i++) 8 | { 9 | A[i]=A[i]+32; 10 | } 11 | cout<<"Changing upper case to lower "< 2 | using namespace std; 3 | int main () 4 | { 5 | char A[]="shivendra"; 6 | int i; 7 | for (i=0;A[i]!='\0';i++) 8 | { 9 | A[i]=A[i]-32; 10 | } 11 | cout << "Changing in upper case"<< endl<< A; 12 | } 13 | -------------------------------------------------------------------------------- /String/04 Upper to Lower & Lower to upper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main () 4 | { 5 | char A[]="ShIvEnDRa"; 6 | int i; 7 | for (i=0;A[i]!='\0';i++) 8 | { 9 | if (A[i]>=65 && A[i]<90) 10 | A[i]+=32; // This is for changing lower case 11 | else if (A[i]>=97 && A[i]<=122) 12 | A[i]-=32; // This is for changing upper case 13 | } 14 | cout<<"Changing Upper to lower and lower to upper "< 2 | using namespace std; 3 | int main () 4 | { 5 | char A[]="I Am From Darbhanga Bihar"; 6 | int i,vcount=0,ccount=0; 7 | for (i=0;A[i]!='\0';i++) 8 | 9 | if (A[i]=='a'|| A[i]=='e'|| A[i]=='i' || A[i]=='o' || A[i]=='u' || A[i]=='A' || A[i]=='E' || A[i]=='I'|| A[i]=='O'|| A[i]=='U' ) 10 | vcount++; 11 | else if ((A[i]>=65 && A[i]<=90) || (A[i]>=97 && A[i]<=122)) 12 | ccount++; 13 | cout<<"Vowel count is "< 2 | using namespace std; 3 | int main() 4 | { 5 | char A[]="My name is Shivendra"; 6 | int i,word=1; 7 | for (i=0;A[i]!='\0';i++) 8 | { 9 | if (A[i]==' '&& A[i-1]!=' ') // if whitespace encounter in string it will count word // 2nd cond if more then one whitespace btw 2 conseqitive word it will eliminate 10 | word++; 11 | } 12 | cout<<"Total word is "< 2 | using namespace std; 3 | void Reverse(char s[], int no) 4 | { 5 | char* B; // string in heap 6 | B = new char[no]; 7 | int i, j; 8 | for (i = 0; s[i] != '\0'; i++) // taking i upto last 9 | { 10 | 11 | } 12 | i = i - 1; // setting i one last of string 13 | for (j = 0; i >= 0; i--, j++) // incrementing j and decrementing i upto i is greater then 0 14 | B[j] = s[i]; 15 | B[j] = '\0'; 16 | cout << "The reverse of string is " << B; 17 | } 18 | void Reverse1(char s[]) 19 | { 20 | int i, temp, j; 21 | for (j = 0; s[j] != '\0'; j++) // taking j to last of the string 22 | { 23 | } 24 | j = j - 1; // setting j one before the last string 25 | for (i = 0; i < j; i++, j--) 26 | { 27 | temp = s[i]; // swapping 1st char with last everytime 28 | s[i] = s[j]; 29 | s[j] = temp; 30 | } 31 | cout << s; 32 | } 33 | 34 | int main() 35 | { 36 | char* s; 37 | int l; 38 | cout << "Enter the Size of the Array " << endl; 39 | cin >> l; 40 | s = new char[l]; 41 | cout << "Enter the String " << endl; 42 | cin >> s; 43 | 44 | Reverse(s, l); 45 | cout< 2 | using namespace std; 3 | int Valid(char s[]) 4 | { 5 | int i; 6 | for (i = 0; s[i] != '\0'; i++) 7 | { 8 | if (!(s[i] >= 65 && s[i] <= 90) && !(s[i] >= 97 && s[i] <= 122) && !(s[i] >= 48 && s[i] <= 57)) 9 | return 0; 10 | } 11 | return 1; 12 | } 13 | int main() 14 | { 15 | char* s; 16 | int l; 17 | cout << "Enter the Size of the Array " << endl; 18 | cin >> l; 19 | s = new char[l]; 20 | cout << "Enter the String " << endl; 21 | cin >> s; 22 | 23 | if (Valid(s) == 1) 24 | cout << "String is valid"; 25 | else 26 | cout << "String is invalid"; 27 | 28 | 29 | 30 | 31 | return 0; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /String/09 Compare Two string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void compare(char s[], char s1[]) 7 | { 8 | int i,j; 9 | for (i = 0, j = 0; s[i] != '\0' && s1[j] != '\0'; i++, j++) 10 | { 11 | if (s[i] != s1[j]) 12 | break; 13 | } 14 | if (s[i] == s1[j]) 15 | cout << "Both the strings are equal " << endl; 16 | else if (s[i] < s1[j]) 17 | cout << "1st one is smaller " << endl; 18 | else 19 | cout << "1st is larger " << endl; 20 | } 21 | 22 | 23 | int main() 24 | { 25 | char* s; 26 | char* s1; 27 | int l; 28 | cout << "Enter the Size of the Array " << endl; 29 | cin >> l; 30 | s = new char[l]; 31 | s1 = new char[l]; 32 | cout << "Enter the 1st String " << endl; 33 | cin >> s; 34 | cout << "Enter the 2nd string " << endl; 35 | cin >> s1; 36 | compare(s, s1); 37 | 38 | return 0; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /String/10 Palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char palindrome(char Arr1[]) 7 | { 8 | int i, j, k = 0; 9 | for (i = 0; Arr1[i] != '\0'; i++) {} 10 | j = i - 1; 11 | while (k <= j) 12 | { 13 | if (Arr1[k] != Arr1[j]) 14 | { 15 | printf("Not a palindrome."); 16 | return 0; // stop the function here 17 | } 18 | else 19 | { 20 | k++; 21 | j--; 22 | } 23 | 24 | } 25 | printf("Palindrome.\n"); 26 | return 0; 27 | } 28 | 29 | 30 | int main() 31 | { 32 | char* s; 33 | int l; 34 | cout << "Enter the Size of the Array " << endl; 35 | cin >> l; 36 | s = new char[l]; 37 | cout << "Enter the 1st String " << endl; 38 | cin >> s; 39 | 40 | palindrome(s); 41 | 42 | return 0; 43 | 44 | } 45 | -------------------------------------------------------------------------------- /String/problem on string/01 Length of string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Length(char s[]) 5 | { 6 | int i, length=0; 7 | for (i = 0; s[i] != '\0'; i++) 8 | { 9 | length++; 10 | } 11 | return length; 12 | } 13 | 14 | int main() 15 | { 16 | char* s; 17 | int l; 18 | cout << "Enter the Size of the Array " << endl; 19 | cin >> l; 20 | s = new char[l]; 21 | cout << "Enter the String " << endl; 22 | cin >> s; 23 | 24 | cout<<"The Length of the string is "< 2 | using namespace std; 3 | 4 | int Length(char s[]) 5 | { 6 | int i, length=0; 7 | for (i = 0; s[i] != '\0'; i++) 8 | { 9 | length++; 10 | } 11 | return length; 12 | } 13 | 14 | void UtoLCase(char s[]) 15 | { 16 | int i; 17 | for (i = 0; s[i] != '\0'; i++) 18 | { 19 | s[i] += 32; 20 | } 21 | cout <<"Changing uppar case to lower case "<= 65 && s[i] <= 90) 39 | { 40 | s[i] += 32; 41 | } 42 | else if (s[i] >= 97 && s[i] <= 122) 43 | { 44 | s[i] -= 32; 45 | } 46 | else 47 | break; 48 | } 49 | cout << s; 50 | } 51 | int main() 52 | { 53 | char* s; 54 | int l; 55 | cout << "Enter the Size of the Array " << endl; 56 | cin >> l; 57 | s = new char[l]; 58 | cout << "Enter the String " << endl; 59 | cin >> s; 60 | 61 | /*cout<<"The Length of the string is "< 2 | using namespace std; 3 | bool isVowel(char c) 4 | { 5 | return (c=='a' || c=='A' || c=='e' || 6 | c=='E' || c=='i' || c=='I' || 7 | c=='o' || c=='O' || c=='u' || 8 | c=='U'); 9 | } 10 | string reverseVowel(string str) 11 | { 12 | int j=0; 13 | string vowel; 14 | for (int i=0; str[i]!='\0'; i++) 15 | if (isVowel(str[i])) 16 | vowel[j++] = str[i]; 17 | for (int i=0; str[i]!='\0'; i++) 18 | if (isVowel(str[i])) 19 | str[i] = vowel[--j] ; 20 | 21 | return str; 22 | } 23 | int main() 24 | { 25 | string str; 26 | cin>>str; 27 | cout << reverseVowel(str); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Tree/Search Trees/01: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | -------------------------------------------------------------------------------- /src/Algorithms/Divide & Conquer/02 Quicksort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Array 5 | { 6 | int* A; 7 | int size; 8 | int length; 9 | }; 10 | 11 | void Swap(int* x, int* y) 12 | { 13 | int temp = *x; 14 | *x = *y; 15 | *y = temp; 16 | } 17 | 18 | void Display(struct Array* arr) 19 | { 20 | int i; 21 | for (i = 0; i < arr->length; i++) 22 | cout << arr->A[i]<<" "; 23 | 24 | } 25 | int Partition(struct Array* arr, int l, int h) 26 | { 27 | int pivot = arr->A[l]; 28 | int i = l,j=h; 29 | 30 | do 31 | { 32 | do { i++; } while (arr->A[i] <= pivot); 33 | do { j--; } while (arr->A[j] > pivot); 34 | if (i < j) 35 | Swap(&arr->A[i], &arr->A[j]); 36 | 37 | } while (i < j); 38 | Swap(&arr->A[l], &arr->A[j]); 39 | return j; 40 | } 41 | 42 | void Quicksort(struct Array* arr, int l, int h) 43 | { 44 | int j; 45 | if (l < h) 46 | { 47 | j= Partition(arr, l, h); 48 | Quicksort(arr, l, j - 1); 49 | Quicksort(arr, j + 1, h); 50 | 51 | } 52 | } 53 | 54 | int main() 55 | { 56 | struct Array arr; 57 | int no; 58 | cout << "Enter the size of the array " << endl; 59 | cin >> arr.size; 60 | arr.A = new int[arr.size]; 61 | cout << "Enter the length of the array " << endl; 62 | cin >> no; 63 | arr.length = 0; 64 | 65 | 66 | 67 | cout << "Enter the emements of the array " << endl; 68 | for (int i = 0; i < no; i++) 69 | cin >> arr.A[i]; 70 | arr.length = no; 71 | Quicksort(&arr, 0, no); 72 | Display(&arr); 73 | return 0; 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/Algorithms/Divide & Conquer/03 Merge sort.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums, int l, int m, int r) 4 | { 5 | int n1 = m - l + 1; 6 | int n2 = r - m; 7 | int A[n1], B[n2]; 8 | 9 | for(int i = 0; i < n1; i++) 10 | A[i] = nums[l + i]; 11 | 12 | for(int i = 0; i < n2; i++) 13 | B[i] = nums[m + 1 + i]; 14 | 15 | int i = 0, j = 0; 16 | int k = l; 17 | 18 | while(i < n1 && j < n2) 19 | { 20 | if(A[i] <= B[j]) 21 | nums[k++] = A[i++]; 22 | else 23 | nums[k++] = B[j++]; 24 | } 25 | 26 | while(i < n1) 27 | nums[k++] = A[i++]; 28 | while(j < n2) 29 | nums[k++] = B[j++]; 30 | } 31 | 32 | void mergeSort(vector& nums, int l, int r) 33 | { 34 | if(l >= r) //remember to put the equal to sign 35 | return; 36 | 37 | int m = l + (r - l)/2; 38 | mergeSort(nums, l, m); 39 | mergeSort(nums, m + 1, r); 40 | merge(nums, l, m, r); 41 | } 42 | 43 | vector sortArray(vector& nums) 44 | { 45 | mergeSort(nums, 0, nums.size() - 1); 46 | return nums; 47 | } 48 | 49 | 50 | }; 51 | -------------------------------------------------------------------------------- /src/Algorithms/Divide & Conquer/readme.md: -------------------------------------------------------------------------------- 1 | ## Reference 2 | * [Divide and Conquer Algorithm](https://www.geeksforgeeks.org/divide-and-conquer-algorithm-introduction/) 3 | * [Divide and Conquer](https://www.tutorialspoint.com/data_structures_algorithms/divide_and_conquer.htm) -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/01 Recursive Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Knapsack(int wt[], int val[], int W, int n) { 5 | // every recursive solution will have a base condition 6 | // for base condition we need to think of the smallest valid input that we can pass 7 | // array size can be atleast 0 || min weight can be 0 but not negetive; 8 | if (n == 0 || W == 0) 9 | return 0; 10 | 11 | // these are the choices we are having 12 | if (wt[n - 1] <= W) { 13 | return max(val[n - 1] + Knapsack(wt, val, W - wt[n - 1], n - 1), 14 | Knapsack(wt, val, W, n - 1)); 15 | } 16 | else if (wt[n - 1] > W) // if the weight is greater then the required weight there is no sence for taking that value. 17 | return Knapsack(wt, val, W, n - 1); // return as it is by redusing the size of array 18 | else 19 | return -1; 20 | } 21 | 22 | int main() { 23 | int n,W; 24 | cin >> n; // number of items 25 | int val[n], wt[n]; // values and weights of array 26 | for (int i = 0; i < n; i++) 27 | cin >> wt[i]; 28 | for (int i = 0; i < n; i++) 29 | cin >> val[i]; 30 | 31 | cin >> W; // Knapsack capacity 32 | 33 | cout << Knapsack(wt, val, W, n) << endl; 34 | return 0; 35 | } 36 | // T(N) = 2T(N-1) + O(1), which is simplified to O(2^N). 37 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/02 Knapsack Memoization(DP).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int D = 1000; // DP - matrix dimension 5 | 6 | int t[D][D]; // DP matrix 7 | 8 | int Knapsack(int wt[], int val[], int W, int n) { 9 | // base case 10 | if (n == 0 || W == 0) 11 | return 0; 12 | 13 | // if already calculated 14 | 15 | 16 | if (t[n][W] != -1) 17 | return t[n][W]; 18 | 19 | // else calculate 20 | else { 21 | if (wt[n - 1] <= W) 22 | t[n][W] = max(val[n - 1] + Knapsack(wt, val, W - wt[n - 1], n - 1),Knapsack(wt, val, W, n - 1)); 23 | else if (wt[n - 1] > W) 24 | t[n][W] = Knapsack(wt, val, W, n - 1); 25 | 26 | return t[n][W]; 27 | } 28 | } 29 | 30 | signed main() { 31 | int n; cin >> n; // number of items 32 | int val[n], wt[n]; // values and wts array 33 | for (int i = 0; i < n; i++) 34 | cin >> wt[i]; 35 | for (int i = 0; i < n; i++) 36 | cin >> val[i]; 37 | int W; cin >> W; // capacity 38 | 39 | // matrix initialization 40 | for (int i = 0; i <= n; i++) 41 | for (int j = 0; j <= W; j++) 42 | t[i][j] = -1; // initialize matrix with -1 43 | 44 | cout << Knapsack(wt, val, W, n) << endl; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/09 Target sum.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public: 4 | int CountSubsetsWithSum(vector& nums,int sum) { 5 | int n= nums.size(); 6 | int t[n + 1][sum + 1]; // DP - matrix 7 | // initialization 8 | t[0][0] = 1; 9 | int k = 1; 10 | for (int i = 0; i <= n; i++) { 11 | for (int j = 0; j <= sum; j++) { 12 | if (i == 0 && j > 0) 13 | t[i][j] = 0; 14 | if (j == 0 && i > 0) { 15 | if (nums[i - 1] == 0) { 16 | t[i][j] = pow(2, k); 17 | k++; 18 | } 19 | else 20 | t[i][j] = t[i - 1][j]; 21 | } 22 | } 23 | } 24 | 25 | for (int i = 1; i <= n; i++) { 26 | for (int j = 1; j <= sum; j++) { 27 | if (nums[i - 1] <= j) 28 | t[i][j] = t[i - 1][j - nums[i - 1]] + t[i - 1][j]; 29 | else 30 | t[i][j] = t[i - 1][j]; 31 | } 32 | } 33 | 34 | return t[n][sum]; 35 | } 36 | int findTargetSumWays(vector& nums, int diff) { 37 | int n= nums.size(); 38 | int sumOfArray = 0; 39 | for (int i = 0; i < n; i++) 40 | sumOfArray += nums[i]; 41 | 42 | if ((sumOfArray + diff) % 2 != 0) 43 | return 0; 44 | else 45 | return CountSubsetsWithSum(nums, (sumOfArray + diff) / 2); 46 | 47 | 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/10 Unbounded Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Un_knapsack(int wt[], int val[], int W, int n) { 5 | int t[n + 1][W + 1]; // DP matrix 6 | 7 | for (int i = 0; i <= n; i++) { 8 | for (int j = 0; j <= W; j++) { 9 | if (i == 0 || j == 0) // base case 10 | t[i][j] = 0; 11 | else if (wt[i - 1] <= j) { // current wt can fit in bag 12 | int val1 = val[i - 1] + t[i][j - wt[i - 1]]; // take current wt 13 | int val2 = t[i - 1][j]; // skip current wt 14 | t[i][j] = max(val1, val2); 15 | } 16 | else if (wt[i - 1] > j) // current wt doesn't fit in bag 17 | t[i][j] = t[i - 1][j]; 18 | } 19 | } 20 | 21 | return t[n][W]; 22 | } 23 | 24 | signed main() { 25 | int n; cin >> n; // number of items 26 | int val[n], wt[n]; // values and wts array 27 | for (int i = 0; i < n; i++) 28 | cin >> wt[i]; 29 | for (int i = 0; i < n; i++) 30 | cin >> val[i]; 31 | int W; cin >> W; // capacity 32 | 33 | cout << Un_knapsack(wt, val, W, n) << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/11 Rod cutting problem.cpp: -------------------------------------------------------------------------------- 1 | //https://www.geeksforgeeks.org/cutting-a-rod-dp-13/ 2 | #include 3 | using namespace std; 4 | 5 | int getMaxProfit(int length[], int price[], int n, int L) { 6 | int dp[n + 1][L + 1]; 7 | for (int i = 0; i <= n; i++) 8 | for (int j = 0; j <= L; j++) 9 | if (j == 0 || i == 0) 10 | dp[i][j] = 0; 11 | 12 | for (int i = 1; i <= n; i++) { 13 | for (int j = 1; j <= L; j++) { 14 | if (length[i - 1] <= j) { 15 | dp[i][j] = max(dp[i - 1][j], 16 | price[i - 1] + dp[i][j - length[i - 1]]); 17 | } 18 | else 19 | dp[i][j] = dp[i - 1][j]; 20 | } 21 | } 22 | 23 | return dp[n][L]; 24 | } 25 | 26 | signed main() { 27 | int n; cin >> n; 28 | int length[n], price[n]; 29 | for (int i = 0; i < n; i++) 30 | cin >> length[i]; 31 | for (int i = 0; i < n; i++) 32 | cin >> price[i]; 33 | int L; cin >> L; 34 | 35 | cout << getMaxProfit(length, price, n, L) << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/12 Coin change problem : maximum no of ways.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getMaxNumberOfWays(int coins[], int n, int sum) { 5 | int t[n + 1][sum + 1]; 6 | // initialization 7 | for (int i = 0; i <= n; i++) { 8 | for (int j = 0; j <= sum; j++) { 9 | if (i == 0) 10 | t[i][j] = 0; 11 | if (j == 0) 12 | t[i][j] = 1; 13 | } 14 | } 15 | 16 | for (int i = 1; i <= n; i++) 17 | for (int j = 1; j <= sum; j++) 18 | if (coins[i - 1] <= j) 19 | t[i][j] = t[i - 1][j] + t[i][j - coins[i - 1]]; 20 | else 21 | t[i][j] = t[i - 1][j]; 22 | 23 | return t[n][sum]; 24 | } 25 | 26 | signed main() { 27 | int n; cin >> n; 28 | int coins[n]; 29 | for (int i = 0; i < n; i++) 30 | cin >> coins[i]; 31 | int sum; cin >> sum; 32 | 33 | cout << getMaxNumberOfWays(coins, n, sum) << endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/13 Coin change problem: Minimum number of coin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define INF INT_MAX-1 4 | 5 | int getMinNumberOfCoins(int coins[], int n, int sum) { 6 | int t[n + 1][sum + 1]; 7 | // initialization 8 | for (int i = 0; i <= n; i++) { 9 | for (int j = 0; j <= sum; j++) { 10 | if (j == 0) 11 | t[i][j] = 0; 12 | if (i == 0) 13 | t[i][j] = INF; 14 | if (i == 1) { 15 | if (j % coins[i - 1] == 0) 16 | t[i][j] = j / coins[i - 1]; 17 | else 18 | t[i][j] = INF; 19 | } 20 | } 21 | } 22 | 23 | t[0][0] = 0; 24 | 25 | for (int i = 1; i <= n; i++) 26 | for (int j = 1; j <= sum; j++) 27 | if (coins[i - 1] <= j) 28 | t[i][j] = min(t[i - 1][j], 1 + t[i][j - coins[i - 1]]); 29 | else 30 | t[i][j] = t[i - 1][j]; 31 | 32 | return t[n][sum]; 33 | } 34 | 35 | signed main() { 36 | int n; cin >> n; 37 | int coins[n]; 38 | for (int i = 0; i < n; i++) 39 | cin >> coins[i]; 40 | int sum; cin >> sum; 41 | 42 | cout << getMinNumberOfCoins(coins, n, sum) << endl; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/14 Longest Common Subsequence recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int LCS(string X, string Y, int n, int m) { 5 | // base case 6 | if (n == 0 || m == 0) 7 | return 0; 8 | 9 | // choice diagram 10 | // when both string X and Y is having same last char 11 | if (X[n - 1] == Y[m - 1]) 12 | return 1 + LCS(X, Y, n - 1, m - 1); // count the number and decreament the both's string length 13 | // when both string's last character is not same 14 | else 15 | return max(LCS(X, Y, n - 1, m), LCS(X, Y, n, m - 1)); // one take full and another by leaving last and vice versa 16 | } 17 | 18 | int main() { 19 | string X, Y; cin >> X >> Y; 20 | int n = X.length(), m = Y.length(); 21 | 22 | cout << LCS(X, Y, n, m) << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/15 Longest Common Subsequence Top down(Memoization).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int dp[1001][1001]; 6 | 7 | int LCS(string X, string Y, int n, int m) { 8 | // base case 9 | if (n == 0 || m == 0) 10 | dp[n][m] = 0; 11 | 12 | if (dp[n][m] != -1) // when table is not having -1 then return the value which is preseent in that block 13 | return dp[n][m]; 14 | 15 | // choice diagram 16 | // when last character is same 17 | if (X[n - 1] == Y[m - 1]) 18 | dp[n][m] = 1 + LCS(X, Y, n - 1, m - 1);// count the number and decreament the both's string length // store the value in particular block 19 | // when last character is not same -> pick max 20 | else 21 | dp[n][m] = max(LCS(X, Y, n - 1, m), LCS(X, Y, n, m - 1)); // one take full and another by leaving last char and vice versa // store the value in particular block 22 | 23 | return dp[n][m]; 24 | } 25 | 26 | int main() { 27 | string X, Y; cin >> X >> Y; 28 | int n = X.length(), m = Y.length(); 29 | 30 | memset(dp, -1, sizeof(dp)); // intialize the whole dp matrix with -1; // from memset we can initialise either -1 and zero; 31 | 32 | cout << LCS(X, Y, n, m) << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/16 Longest Common Subsequence Bottom Up(DP).cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int LCS(string X, string Y, int n, int m) { 6 | int dp[n + 1][m + 1]; // DP - matrix 7 | 8 | // initialize 1st row and of dp matrix with 0 according to the base condition of recursion // base case of recursion --> for initialization of dp - matrix 9 | for (int i = 0; i <= n; i++) 10 | for (int j = 0; j <= m; j++) 11 | if (i == 0 || j == 0) 12 | dp[i][j] = 0; 13 | // choise diagram is used to fill rest of the matrix 14 | for (int i = 1; i <= n; i++) 15 | for (int j = 1; j <= m; j++) 16 | if (X[i - 1] == Y[j - 1]) // when last character is same 17 | dp[i][j] = 1 + dp[i - 1][j - 1]; 18 | else // when last character is not same -> pick max 19 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 20 | 21 | return dp[n][m]; // last row and last column element will give the length of the LCS; 22 | } 23 | 24 | int main() { 25 | string X, Y; cin >> X >> Y; 26 | int n = X.length(), m = Y.length(); 27 | 28 | cout << LCS(X, Y, n, m) << endl; 29 | return 0; 30 | } 31 | 32 | // https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/#:~:text=Time%20complexity%20of%20the%20above,length%20of%20LCS%20is%200.&text=In%20the%20above%20partial%20recursion,%E2%80%9D)%20is%20being%20solved%20twice. 33 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/17 LCS Substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int LCSubstr(string X, string Y, int n, int m) { 5 | int dp[n + 1][m + 1]; // DP - matrix 6 | 7 | // base condition 8 | for (int i = 0; i <= n; i++) 9 | for (int j = 0; j <= m; j++) 10 | if (i == 0 || j == 0) 11 | dp[i][j] = 0; // 12 | 13 | // choice diagram 14 | for (int i = 1; i <= n; i++) 15 | for (int j = 1; j <= m; j++) 16 | if (X[i - 1] == Y[j - 1]) // when both string's last char is same 17 | dp[i][j] = 1 + dp[i - 1][j - 1]; // count the number and decrement the table 18 | else 19 | dp[i][j] = 0; // variation from LCS(DP) 20 | 21 | int mx = INT_MIN; 22 | for (int i = 0; i <= n; i++) 23 | for (int j = 0; j <= m; j++) 24 | mx = max(mx, dp[i][j]); 25 | 26 | return mx; 27 | } 28 | 29 | int main() { 30 | string X, Y; cin >> X >> Y; 31 | int n = X.length(), m = Y.length(); 32 | 33 | cout << LCSubstr(X, Y, n, m) << endl; 34 | return 0; 35 | } 36 | 37 | // https://www.geeksforgeeks.org/longest-common-substring-dp-29/ 38 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/18 Print LCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | string LCS(string X, string Y, int n, int m) { 5 | int dp[n + 1][m + 1]; // DP - matrix 6 | 7 | // base case of recursion --> for initialization of dp - matrix 8 | for (int i = 0; i <= n; i++) 9 | for (int j = 0; j <= m; j++) 10 | if (i == 0 || j == 0) 11 | dp[i][j] = 0; 12 | 13 | for (int i = 1; i <= n; i++) 14 | for (int j = 1; j <= m; j++) 15 | if (X[i - 1] == Y[j - 1]) // when last character is same 16 | dp[i][j] = 1 + dp[i - 1][j - 1]; 17 | else // when last character is not same -> pick max 18 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 19 | 20 | int i = n, j = m; 21 | string lcs = ""; // store charecter when it is equal in the table 22 | while (i > 0 && j > 0) { 23 | if (X[i - 1] == Y[j - 1]) { 24 | lcs += X[i - 1]; // insert in string 25 | i--, j--; 26 | } 27 | else { 28 | if (dp[i][j - 1] > dp[i - 1][j]) 29 | j--; // move to the larger side 30 | else 31 | i--; 32 | } 33 | } 34 | reverse(lcs.begin(), lcs.end()); // at last reverse the string to get LCS 35 | 36 | return lcs; 37 | } 38 | 39 | signed main() { 40 | string X, Y; cin >> X >> Y; 41 | int n = X.length(), m = Y.length(); 42 | 43 | cout << LCS(X, Y, n, m) << endl; 44 | return 0; 45 | } 46 | // https://www.geeksforgeeks.org/printing-longest-common-subsequence/ 47 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/19 Shortest Common Supersequence.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int LCS(string X, string Y, int n, int m) { 6 | int dp[n + 1][m + 1]; // DP - matrix 7 | 8 | // base case of recursion --> for initialization of dp - matrix 9 | for (int i = 0; i <= n; i++) 10 | for (int j = 0; j <= m; j++) 11 | if (i == 0 || j == 0) 12 | dp[i][j] = 0; 13 | 14 | for (int i = 1; i <= n; i++) 15 | for (int j = 1; j <= m; j++) 16 | if (X[i - 1] == Y[j - 1]) // when last character is same 17 | dp[i][j] = 1 + dp[i - 1][j - 1]; 18 | else // when last character is not same -> pick max 19 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 20 | 21 | return dp[n][m]; 22 | } 23 | 24 | int SCS(string X, string Y, int n, int m) { 25 | return m + n - LCS(X, Y, n, m); // formula // n-> length of string x ; m-> length of string y 26 | } 27 | 28 | signed main() { 29 | string X, Y; cin >> X >> Y; 30 | int n = X.length(), m = Y.length(); 31 | 32 | cout << SCS(X, Y, n, m) << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/20 Minimum insertion deletion to convert a to b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int LCS(string X, string Y, int n, int m) { 5 | int dp[n + 1][m + 1]; // DP - matrix 6 | 7 | // base case of recursion --> for initialization of dp - matrix 8 | for (int i = 0; i <= n; i++) 9 | for (int j = 0; j <= m; j++) 10 | if (i == 0 || j == 0) 11 | dp[i][j] = 0; 12 | 13 | for (int i = 1; i <= n; i++) 14 | for (int j = 1; j <= m; j++) 15 | if (X[i - 1] == Y[j - 1]) // when last character is same 16 | dp[i][j] = 1 + dp[i - 1][j - 1]; 17 | else // when last character is not same -> pick max 18 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 19 | 20 | return dp[n][m]; 21 | } 22 | 23 | void MinInsertDel(string X, string Y, int n, int m) { 24 | int lcs_len = LCS(X, Y, n, m); 25 | cout << "Minimum number of deletions = " << (n - lcs_len)< 2 | using namespace std; 3 | 4 | int LCS(string X, string Y, int n, int m) { 5 | int dp[n + 1][m + 1]; // DP - matrix 6 | 7 | // base case of recursion --> for initialization of dp - matrix 8 | for (int i = 0; i <= n; i++) 9 | for (int j = 0; j <= m; j++) 10 | if (i == 0 || j == 0) 11 | dp[i][j] = 0; 12 | 13 | for (int i = 1; i <= n; i++) 14 | for (int j = 1; j <= m; j++) 15 | if (X[i - 1] == Y[j - 1]) // when last character is same 16 | dp[i][j] = 1 + dp[i - 1][j - 1]; 17 | else // when last character is not same -> pick max 18 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 19 | 20 | return dp[n][m]; 21 | } 22 | 23 | int LPS(string X, int n) { 24 | string rev_X = X; 25 | reverse(rev_X.begin(), rev_X.end()); 26 | return LCS(X, rev_X, n, n); 27 | } 28 | 29 | int MinInsertForPallindrome(string X, int n) { 30 | return n - LPS(X, n); // substract LPS from the length of string to get Minimum number of insertion to make a string palindrome 31 | } 32 | 33 | int main() { 34 | string X, Y; cin >> X; 35 | int n = X.length(); 36 | 37 | cout << MinInsertForPallindrome(X, n) << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/27 Matrix chain multiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solve(int arr[], int i, int j) { 5 | if (i >= j) 6 | return 0; 7 | 8 | int ans = INT_MAX; 9 | for (int k = i; k <= j - 1; k++) { 10 | int temp_ans = Solve(arr, i, k) + Solve(arr, k + 1, j) + arr[i - 1] * arr[k] * arr[j]; 11 | ans = min(ans, temp_ans); // take temp minimum value from the temp answers 12 | } 13 | 14 | return ans; 15 | } 16 | 17 | signed main() { 18 | int n; cin >> n; 19 | int arr[n]; 20 | for (int i = 0; i < n; i++) 21 | cin >> arr[i]; 22 | 23 | cout << Solve(arr, 1, n - 1) << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/28 MCM Memoization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int D = 1000; 5 | int t[D][D]; 6 | 7 | int Solve(int arr[], int i, int j) { 8 | if (i >= j) { 9 | t[i][j] = 0; 10 | return 0; 11 | } 12 | 13 | if (t[i][j] != -1)// when it is not zero means return the value from the table other than -1 14 | return t[i][j]; 15 | 16 | int ans = INT_MAX; 17 | for (int k = i; k <= j - 1; k++) { 18 | int temp_ans = Solve(arr, i, k) + Solve(arr, k + 1, j) + arr[i - 1] * arr[k] * arr[j]; 19 | ans = min(ans, temp_ans); 20 | } 21 | 22 | return t[i][j] = ans; // store it in table 23 | } 24 | 25 | signed main() { 26 | int n; cin >> n; 27 | int arr[n]; 28 | for (int i = 0; i < n; i++) 29 | cin >> arr[i]; 30 | 31 | memset(t, -1, sizeof(t)); 32 | 33 | cout << Solve(arr, 1, n - 1) << endl; 34 | return 0; 35 | } 36 | //https://www.techiedelight.com/matrix-chain-multiplication/ 37 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/29 Palindrome Partitioning Recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPallindrome(string X, int i, int j) { // function to check either a string is palindrome or not 5 | while (i <= j) { 6 | if (X[i] != X[j]) 7 | return false; 8 | i++, j--; 9 | } 10 | 11 | return true; 12 | } 13 | 14 | int Solve(string X, int i, int j) { 15 | if (i >= j || isPallindrome(X, i, j)) 16 | return 0; 17 | 18 | int ans = INT_MAX; 19 | for (int k = i; k < j; k++) { 20 | int temp_ans = Solve(X, i, k) + Solve(X, k + 1, j) + 1; 21 | ans = min(ans, temp_ans); 22 | } 23 | 24 | return ans; 25 | } 26 | 27 | int main() { 28 | string X; cin >> X; 29 | 30 | cout << Solve(X, 0, X.length() - 1) << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/30 Palindrome Partitioning Memoization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int D = 1001; 5 | int t[D][D]; 6 | 7 | bool isPallindrome(string X, int i, int j) { 8 | while (i <= j) { 9 | if (X[i] != X[j]) 10 | return false; 11 | i++, j--; 12 | } 13 | 14 | return true; 15 | } 16 | 17 | int Solve(string X, int i, int j) { 18 | if (i >= j || isPallindrome(X, i, j)) { 19 | t[i][j] = 0; 20 | return 0; 21 | } 22 | 23 | if (t[i][j] != -1) 24 | return t[i][j]; 25 | 26 | int ans = INT_MAX; 27 | for (int k = i; k < j; k++) { 28 | int temp_ans = Solve(X, i, k) + Solve(X, k + 1, j) + 1; 29 | ans = min(ans, temp_ans); 30 | } 31 | 32 | return t[i][j] = ans; 33 | } 34 | 35 | int main() { 36 | string X; cin >> X; 37 | 38 | memset(t, -1, sizeof(t)); 39 | 40 | cout << Solve(X, 0, X.length() - 1) << endl; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/31 Palindrome Partitioning Memoized optimization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int D = 1001; 5 | int t[D][D]; 6 | 7 | bool isPallindrome(string X, int i, int j) { 8 | while (i <= j) { 9 | if (X[i] != X[j]) 10 | return false; 11 | i++, j--; 12 | } 13 | 14 | return true; 15 | } 16 | 17 | int Solve(string X, int i, int j) { 18 | if (t[i][j] != -1) 19 | return t[i][j]; 20 | 21 | if (i >= j || isPallindrome(X, i, j)) { 22 | t[i][j] = 0; 23 | return 0; 24 | } 25 | 26 | int ans = INT_MAX; 27 | for (int k = i; k < j; k++) { 28 | int left, right; 29 | if (t[i][k] == -1) 30 | left = Solve(X, i, k); 31 | else 32 | left = t[i][k]; 33 | 34 | if (t[k + 1][j] == -1) 35 | right = Solve(X, k + 1, j); 36 | else 37 | right = t[k + 1][j]; 38 | 39 | int temp_ans = left + right + 1; 40 | ans = min(ans, temp_ans); 41 | } 42 | 43 | return t[i][j] = ans; 44 | } 45 | 46 | int main() { 47 | string X; cin >> X; 48 | 49 | memset(t, -1, sizeof(t)); 50 | 51 | cout << Solve(X, 0, X.length() - 1) << endl; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/32 Evaluate Expression to true Recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solve(string X, int i, int j, bool isTrue) { 5 | if (i >= j) { 6 | if (isTrue) 7 | return X[i] == 'T'; 8 | else 9 | return X[i] == 'F'; 10 | } 11 | 12 | int ans = 0; 13 | for (int k = i + 1; k < j; k += 2) { 14 | int l_T = Solve(X, i, k - 1, true); 15 | int l_F = Solve(X, i, k - 1, false); 16 | int r_T = Solve(X, k + 1, j, true); 17 | int r_F = Solve(X, k + 1, j, false); 18 | 19 | if (X[k] == '|') { 20 | if (isTrue == true) 21 | ans += l_T * r_T + l_T * r_F + l_F * r_T; 22 | else 23 | ans += l_F * r_F; 24 | } 25 | else if (X[k] == '&') { 26 | if (isTrue == true) 27 | ans += l_T * r_T; 28 | else 29 | ans += l_T * r_F + l_F * r_T + l_F * r_F; 30 | } 31 | else if (X[k] == '^') { 32 | if (isTrue == true) 33 | ans += l_T * r_F + l_F * r_T; 34 | else 35 | ans += l_T * r_T + l_F * r_F; 36 | } 37 | 38 | } 39 | 40 | return ans; 41 | } 42 | 43 | int main() { 44 | string X; cin >> X; 45 | cout << Solve(X, 0, X.length() - 1, true) << endl; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/33 Evaluate expression to true memoization using map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unordered_map ump; 5 | 6 | int Solve(string X, int i, int j, bool isTrue) { 7 | string key = to_string(i) + " " + to_string(j) + " " + (isTrue ? "T" : "F"); 8 | 9 | if (ump.find(key) != ump.end()) 10 | return ump[key]; 11 | 12 | if (i >= j) { 13 | if (isTrue) 14 | ump[key] = X[i] == 'T'; 15 | else 16 | ump[key] = X[i] == 'F'; 17 | return ump[key]; 18 | } 19 | 20 | int ans = 0; 21 | for (int k = i + 1; k < j; k += 2) { 22 | int l_T = Solve(X, i, k - 1, true); 23 | int l_F = Solve(X, i, k - 1, false); 24 | int r_T = Solve(X, k + 1, j, true); 25 | int r_F = Solve(X, k + 1, j, false); 26 | 27 | if (X[k] == '|') { 28 | if (isTrue == true) 29 | ans += l_T * r_T + l_T * r_F + l_F * r_T; 30 | else 31 | ans += l_F * r_F; 32 | } 33 | else if (X[k] == '&') { 34 | if (isTrue == true) 35 | ans += l_T * r_T; 36 | else 37 | ans += l_T * r_F + l_F * r_T + l_F * r_F; 38 | } 39 | else if (X[k] == '^') { 40 | if (isTrue == true) 41 | ans += l_T * r_F + l_F * r_T; 42 | else 43 | ans += l_T * r_T + l_F * r_F; 44 | } 45 | 46 | } 47 | 48 | return ump[key] = ans; 49 | } 50 | 51 | signed main() { 52 | string X; cin >> X; 53 | ump.clear(); 54 | cout << Solve(X, 0, X.length() - 1, true) << endl; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/34 Evaluate expression to true memoization using 3d array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int D = 1001; 5 | int dp[2][D][D]; // i,j and istrue and false is changing 6 | 7 | int Solve(string X, int i, int j, bool isTrue) { 8 | if (i >= j) { 9 | if (isTrue) 10 | dp[1][i][j] = X[i] == 'T'; 11 | else 12 | dp[0][i][j] = X[i] == 'F'; 13 | return dp[isTrue][i][j]; 14 | } 15 | 16 | if (dp[isTrue][i][j] != -1) 17 | return dp[isTrue][i][j]; 18 | 19 | int ans = 0; 20 | for (int k = i + 1; k < j; k += 2) { 21 | int l_T = Solve(X, i, k - 1, true); 22 | int l_F = Solve(X, i, k - 1, false); 23 | int r_T = Solve(X, k + 1, j, true); 24 | int r_F = Solve(X, k + 1, j, false); 25 | 26 | if (X[k] == '|') { 27 | if (isTrue == true) 28 | ans += l_T * r_T + l_T * r_F + l_F * r_T; 29 | else 30 | ans += l_F * r_F; 31 | } 32 | else if (X[k] == '&') { 33 | if (isTrue == true) 34 | ans += l_T * r_T; 35 | else 36 | ans += l_T * r_F + l_F * r_T + l_F * r_F; 37 | } 38 | else if (X[k] == '^') { 39 | if (isTrue == true) 40 | ans += l_T * r_F + l_F * r_T; 41 | else 42 | ans += l_T * r_T + l_F * r_F; 43 | } 44 | 45 | } 46 | 47 | dp[isTrue][i][j] = ans; 48 | 49 | return ans; 50 | } 51 | 52 | int main() { 53 | string X; cin >> X; 54 | 55 | memset(dp[0], -1, sizeof(dp[0])); 56 | memset(dp[1], -1, sizeof(dp[1])); 57 | 58 | cout << Solve(X, 0, X.length() - 1, true) << endl; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/35 Scramble string recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool Solve(string X, string Y) { 5 | if (X.compare(Y) == 0) 6 | return true; 7 | if (X.length() <= 1) 8 | return false; 9 | 10 | int n = X.length(); 11 | int flag = false; 12 | for (int i = 1; i <= n - 1; i++) { 13 | if ((Solve(X.substr(0, i), Y.substr(n - i, i)) && Solve(X.substr(i), Y.substr(0, n - i))) || // these are two condition for swapping and not swapping the string 14 | (Solve(X.substr(0, i), Y.substr(0, i)) && Solve(X.substr(i), Y.substr(i)))) { 15 | flag = true; // change the flag to true and break 16 | break; 17 | } 18 | } 19 | 20 | return flag; 21 | } 22 | 23 | int main() { 24 | string X, Y; cin >> X >> Y; 25 | 26 | if (X.length() != Y.length()) 27 | cout << "No\n"; 28 | else 29 | Solve(X, Y) ? cout << "Yes\n" : cout << "No\n"; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/36 Scramble string top down.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unordered_map ump; 5 | 6 | bool Solve(string X, string Y) { 7 | string key = X + " " + Y; 8 | if (ump.find(key) != ump.end()) // if we did not found the and travesed upto end of the map 9 | return ump[key]; 10 | 11 | if (X.compare(Y) == 0) { 12 | ump[key] = true; 13 | return true; 14 | } 15 | if (X.length() <= 1) { 16 | ump[key] = false; 17 | return false; 18 | } 19 | 20 | int n = X.length(); 21 | int flag = false; 22 | for (int i = 1; i <= n - 1; i++) { 23 | if ((Solve(X.substr(0, i), Y.substr(n - i, i)) && Solve(X.substr(i), Y.substr(0, n - i))) || 24 | (Solve(X.substr(0, i), Y.substr(0, i)) && Solve(X.substr(i), Y.substr(i)))) { 25 | flag = true; 26 | break; 27 | } 28 | } 29 | 30 | return ump[key] = flag; // store in table for further reference 31 | } 32 | 33 | int main() { 34 | string X, Y; cin >> X >> Y; 35 | 36 | ump.clear(); 37 | 38 | if (X.length() != Y.length()) 39 | cout << "No\n"; 40 | else 41 | Solve(X, Y) ? cout << "Yes\n" : cout << "No\n"; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/37 Egg dropping problem recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int Solve(int eggs, int floors) { 5 | if (eggs == 1) 6 | return floors; 7 | if (floors == 0 || floors == 1) 8 | return floors; 9 | 10 | int mn = INT_MAX; 11 | for (int k = 1; k <= floors; k++) { 12 | int temp_ans = 1 + max(Solve(eggs - 1, k - 1), Solve(eggs, floors - k)); // once egg break means decrement both floor and egg another agg did not break means check egg dropping from above 13 | mn = min(mn, temp_ans); 14 | } 15 | 16 | return mn; 17 | } 18 | 19 | int main() { 20 | int eggs, floors; 21 | cin >> eggs >> floors; 22 | 23 | cout << Solve(eggs, floors) << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/38 Egg dropping problem top down.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int D = 101; 5 | int t[D][D]; 6 | 7 | int Solve(int eggs, int floors) { 8 | if (t[eggs][floors] != -1) 9 | return t[eggs][floors]; 10 | 11 | if (eggs == 1 || floors == 0 || floors == 1) { 12 | t[eggs][floors] = floors; 13 | return floors; 14 | } 15 | 16 | int mn = INT_MAX; 17 | for (int k = 1; k <= floors; k++) { 18 | int temp_ans = 1 + max(Solve(eggs - 1, k - 1), Solve(eggs, floors - k)); 19 | mn = min(mn, temp_ans); 20 | } 21 | 22 | return t[eggs][floors] = mn; // store in table for further reference 23 | } 24 | 25 | signed main() { 26 | int eggs, floors; 27 | cin >> eggs >> floors; 28 | 29 | memset(t, -1, sizeof(t)); 30 | 31 | cout << Solve(eggs, floors) << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/39 Egg dropping problem memoization optimization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int D = 101; 5 | int dp[D][D]; 6 | 7 | int Solve(int eggs, int floors) { 8 | if (dp[eggs][floors] != -1) // if value is already there in the table then return the value 9 | return dp[eggs][floors]; 10 | 11 | if (eggs == 1 || floors == 0 || floors == 1) { // base condition 12 | dp[eggs][floors] = floors; 13 | return floors; 14 | } 15 | 16 | int mn = INT_MAX; 17 | for (int k = 1; k <= floors; k++) { 18 | int top, bottom; 19 | if (dp[eggs - 1][k - 1] != -1) // break the temp in sub problemes 20 | top = dp[eggs - 1][k - 1]; 21 | else { 22 | top = Solve(eggs - 1, k - 1); 23 | dp[eggs - 1][k - 1] = top; 24 | } 25 | 26 | if (dp[eggs][floors - k] != -1) 27 | bottom = dp[eggs][floors - k]; 28 | else { 29 | bottom = Solve(eggs, floors - k); 30 | dp[eggs][floors - k] = bottom; 31 | } 32 | int temp_ans = 1 + max(top, bottom); 33 | mn = min(mn, temp_ans); 34 | } 35 | 36 | return dp[eggs][floors] = mn; 37 | } 38 | 39 | int main() { 40 | int eggs, floors; 41 | cin >> eggs >> floors; 42 | 43 | memset(dp, -1, sizeof(dp)); 44 | 45 | cout << Solve(eggs, floors) << endl; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/40 Diameter of binary tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | private: 14 | int res; 15 | public: 16 | int Solve(TreeNode* root) { 17 | if (root == NULL) 18 | return 0; 19 | 20 | int l = Solve(root->left); 21 | int r = Solve(root->right); 22 | 23 | int temp = 1 + max(l, r); 24 | int ans = max(temp, l + r + 1); 25 | res = max(res, ans); 26 | return temp; 27 | } 28 | int diameterOfBinaryTree(TreeNode* root) { 29 | if (root == NULL) 30 | return 0; 31 | 32 | res = INT_MIN + 1; 33 | Solve(root); 34 | return res - 1; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/41 Max path sum from any node to any.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | private: 14 | int res; 15 | public: 16 | int Solve(TreeNode* root) { 17 | if (root == NULL) 18 | return 0; 19 | 20 | int l = Solve(root->left); 21 | int r = Solve(root->right); 22 | 23 | int temp = max(root->val + max(l, r), root->val); 24 | int ans = max(temp, l + r + root->val); 25 | res = max(res, ans); 26 | 27 | return temp; 28 | } 29 | int maxPathSum(TreeNode* root) { 30 | res = INT_MIN; 31 | Solve(root); 32 | return res; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/42 Max_path_sum_from_leaf_to_leaf.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | int res; 4 | 5 | int Solve(Node* root) { 6 | if (root == NULL) 7 | return 0; 8 | 9 | int l = Solve(root->left); 10 | int r = Solve(root->right); 11 | 12 | int temp; 13 | if (root->left && root->right) { 14 | res = max(res, l + r + root->data); 15 | temp = root->data + max(l, r); 16 | } 17 | else if (root->left) 18 | temp = root->data + l; 19 | else if (root->right) 20 | temp = root->data + r; 21 | else 22 | temp = root->data; 23 | } 24 | 25 | return temp; 26 | } 27 | 28 | int maxPathSum(Node* root) 29 | { 30 | // code here 31 | res = INT_MIN; 32 | Solve(root); 33 | return res; 34 | } 35 | -------------------------------------------------------------------------------- /src/Algorithms/Dynamic Programming/readme.md: -------------------------------------------------------------------------------- 1 | ## Reference 2 | * Content for code of this is taken from Aaditya verma DP(YouTube Playlist) 3 | * [Dynamic Programming](https://www.geeksforgeeks.org/dynamic-programming/) 4 | * [Dynamic Programming](https://www.tutorialspoint.com/data_structures_algorithms/dynamic_programming.htm) 5 | -------------------------------------------------------------------------------- /src/Competitive Coding (DSA)/01 Array/01 Maximum SubArray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. 3 | 4 | Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] 11 | Output: 6 12 | Explanation: [4,-1,2,1] has the largest sum = 6. 13 | Example 2: 14 | 15 | Input: nums = [1] 16 | Output: 1 17 | Example 3: 18 | 19 | Input: nums = [0] 20 | Output: 0 21 | Example 4: 22 | 23 | Input: nums = [-1] 24 | Output: -1 25 | Example 5: 26 | 27 | Input: nums = [-2147483647] 28 | Output: -2147483647 29 | 30 | 31 | Constraints: 32 | 33 | 1 <= nums.length <= 2 * 104 34 | -231 <= nums[i] <= 231 - 1 35 | */ 36 | 37 | class Solution { 38 | public: 39 | int maxSubArray(vector& nums) { 40 | int max_sub_array =nums[0], current_sum=0; 41 | for (int i=0;i 2 | using namespace std; 3 | 4 | void Display(int A[], int size) 5 | { 6 | int i; 7 | for (i = 0; i < size; i++) 8 | cout << A[i]<<" "; 9 | } 10 | 11 | void Reverse(int A[], int start, int end) 12 | { 13 | while (start < end) 14 | { 15 | int temp = A[start]; 16 | A[start] = A[end]; 17 | A[end] = temp; 18 | start++; 19 | end--; 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | int A[20], i; 26 | int no = sizeof(int) / sizeof(A[0]); 27 | 28 | cout << "Enter the size of array"<> no; 30 | cout << "Enter all the elements of the arrry"<> A[i]; 33 | Reverse(A, 0, no - 1); 34 | cout << "After reversing array " << endl; 35 | Display(A, no); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /src/Competitive Coding (DSA)/01 Array/05 Move all negative numbers to beginning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Quicksort(int A[], int s) 5 | { 6 | int j = 0, i; 7 | for (i = 0; i < s; i++) 8 | if (A[i] < 0) 9 | if (i != j) { 10 | swap(A[i], A[j]); 11 | j++; 12 | } 13 | 14 | } 15 | void printarray(int A[], int s) 16 | { 17 | int i; 18 | for (i = 0; i < s; i++) 19 | cout << A[i] << " "; 20 | } 21 | int main() 22 | { 23 | int arr[] = { -1, 2, -3, 4, 5, 6, -7, 8, 9 }; 24 | int n = sizeof(arr) / sizeof(arr[0]); 25 | Quicksort(arr, n); 26 | printarray(arr, n); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /src/Competitive Coding (DSA)/01 Array/06 Reverse Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | int reverse(int x) { 5 | long long res = 0; 6 | while(x != 0) 7 | { 8 | res = res*10 + x%10; 9 | x /= 10; 10 | 11 | if(abs(res) > INT_MAX) 12 | return 0; 13 | } 14 | return res; 15 | } 16 | }; 17 | //Input: x = 123 18 | //Output: 321 -------------------------------------------------------------------------------- /src/Hackerrank Contest/04 Negetive Marking.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Raju is giving his JEE Main exam. The exam has Q questions and Raju needs S marks to pass. Giving the correct answer to a question awards the student with 4 marks whereas giving the incorrect answer to a question awards the student with negative 3 (-3) marks. If a student chooses to not answer a question at all, he is awarded 0 marks. 3 | 4 | Write a program to calculate the minimum accuracy that Raju will need in order to pass the exam. 5 | 6 | Input 7 | Input consists of multiple test cases. 8 | Each test case consists of two integers Q and S 9 | 10 | Output 11 | Print the minimum accuracy upto 2 decimal places 12 | Print -1 if it is impossible to pass the exam 13 | 14 | Sample Input 0 15 | 16 | 2 17 | 10 40 18 | 10 33 19 | Sample Output 0 20 | 21 | 100.00 22 | 90.00 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | 33 | int main() { 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | int q, s; 39 | cin>>q>>s; 40 | float x=(s+(3*q))/7.00; 41 | float p=(x/q)*100; 42 | if(s<=(4*q)) 43 | printf("%0.2f\n", p); 44 | else 45 | cout<<"-1"< 27 | #include 28 | #include 29 | #include 30 | #include 31 | using namespace std; 32 | int F(int n,int F0,int F1,int F2) 33 | { 34 | if(n==0) 35 | return F0; 36 | else if(n==1) 37 | return F1; 38 | else if(n==2) 39 | return F2; 40 | else 41 | return F(n-1,F0,F1,F2)^F(n-2,F0,F1,F2)+F(n-3,F0,F1,F2); 42 | } 43 | 44 | int main() { 45 | int f0,f1,f2,n; 46 | cin>>f0>>f1>>f2>>n; 47 | cout< 2 | using namespace std; 3 | 4 | // This program was created by Code With Harry 5 | /* this 6 | is 7 | a 8 | multi 9 | line 10 | comment */ 11 | int main(){ 12 | int sum = 6; 13 | cout<< "Hello world"<< sum; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /src/cpp/00 basic2.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | 3 | using namespace std; 4 | int glo = 6; 5 | void sum(){ 6 | int a; 7 | cout<< glo; 8 | } 9 | 10 | int main(){ 11 | int glo=9; 12 | glo=78; 13 | // int a = 14; 14 | // int b = 15; 15 | int a=14, b=15; 16 | float pi=3.14; 17 | char c ='d'; 18 | bool is_true = false; 19 | sum(); 20 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num1, num2; 7 | cout<<"Enter the value of num1:\n"; /* '<<' is called Insertion operator */ 8 | cin>>num1; /* '>>' is called Extraction operator */ 9 | 10 | cout<<"Enter the value of num2:\n"; /* '<<' is called Insertion operator */ 11 | cin>>num2; /* '>>' is called Extraction operator */ 12 | 13 | cout<<"The sum is "<< num1+num2; 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/cpp/01 Function/01 Function Overloading.cpp: -------------------------------------------------------------------------------- 1 | //Function Overloading 2 | //• If More than one functions can have same name, but different parameter list, then they 3 | //are overloaded functions 4 | //• Return the is not considered in overloading 5 | //• Function overloading is used for achieving compile time polymorphism 6 | 7 | #include 8 | using namespace std; 9 | 10 | int Sum(int a, int b) 11 | { 12 | return a + b; 13 | } 14 | 15 | float Sum(float a, float b) 16 | { 17 | return a + b; 18 | } 19 | 20 | int Sum(int a, int b, int c) 21 | { 22 | return a + b + c; 23 | } 24 | 25 | int main() 26 | { 27 | cout <<"Sum using two parameter "<< Sum(1, 8)< 9 | using namespace std; 10 | template 11 | 12 | T Maxi(T a, T b) 13 | { 14 | return a > b ? a : b; 15 | } 16 | 17 | int main() 18 | { 19 | int a = Maxi(4, 9); 20 | float b = Maxi(41.0f, 64.66f); 21 | cout << "The maximum of the integer number is " << a< 12 | using namespace std; 13 | int sum(int a, int b, int c = 0) 14 | { 15 | return a + b + c; 16 | } 17 | int main() 18 | { 19 | cout << sum(10, 20, 3) << endl; 20 | cout << sum(10, 20) << endl; 21 | cout << sum(10, 20,54) << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /src/cpp/01 Function/Simple exaample.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Functions 3 | //• Function is a module which performs a specific task 4 | //• Functions are called by name 5 | //• Rules for giving function name is same as variable name 6 | //• Function can take 0 or more parameters 7 | //• Function can return single value 8 | //• Void function don’t return any value 9 | //• Default return type is int 10 | 11 | #include 12 | using namespace std; 13 | 14 | void Dispaly() 15 | { 16 | cout << "Hello world " << endl; // dont use cin and cout inside fxn it is not a good habbit 17 | } 18 | 19 | float add(float x, float y) 20 | { 21 | float z; 22 | z = x + y; 23 | return z; 24 | } 25 | 26 | int maxi(int a, int b, int c) 27 | { 28 | if (a > b && a > c) 29 | return a; 30 | else if (b > c) 31 | return b; 32 | else 33 | return c; 34 | } 35 | 36 | int main() 37 | { 38 | //Dispaly(); 39 | 40 | 41 | //float x = 2.3, y = 6.3; 42 | //cout<> a >> b >> c; 47 | d = maxi(a, b, c); 48 | cout << "Maximum is " << d << endl; 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /src/cpp/02 Constants, Manipulators & Operator Precedence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | // int a = 34; 8 | // cout<<"The value of a was: "< 2 | using namespace std; 3 | 4 | class Rectangle 5 | { 6 | public: 7 | int length; // Data members // only data member will occupy memory fxn will not occupy any memory 8 | int breadth; 9 | 10 | int area() {// member function 11 | return length * breadth; 12 | } 13 | int perimeter() { 14 | return 2 * (length + breadth); 15 | } 16 | }; 17 | 18 | void main() 19 | { 20 | Rectangle r1, r2; // total it is going to take 4 bytes (2 for length and 2 for breadth)// here we created two objects for rec class 21 | r1.length = 10; // dot operator is used to access data member of the class 22 | r1.breadth = 5; 23 | cout << r1.area() < 2 | using namespace std; 3 | 4 | class Rectangle 5 | { 6 | public: 7 | int l; 8 | int b; 9 | 10 | int area() 11 | { 12 | return l * b; 13 | } 14 | int perimeter() 15 | { 16 | return 2 * (l + b); 17 | } 18 | }; 19 | 20 | void main() 21 | { 22 | Rectangle r1; 23 | r1.l = 5; 24 | r1.b = 8; 25 | cout << "The Area of the rectangle is " << r1.area() << endl; 26 | cout << "The Perimeter of the rectangle is " << r1.perimeter() << endl; 27 | } -------------------------------------------------------------------------------- /src/cpp/02 OOPS concept/03 pointer to an object.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Rectangle 5 | { 6 | public: 7 | int l; 8 | int b; 9 | 10 | int area() 11 | { 12 | return l * b; 13 | } 14 | int perimeter() 15 | { 16 | return 2 * (l + b); 17 | } 18 | }; 19 | 20 | void main() 21 | { 22 | //Rectangle r1, r2 // this way we can create memory inside the stack 23 | Rectangle *r1; 24 | r1 = new Rectangle; 25 | // with this way we can create space in heap memory 26 | Rectangle* r2 = new Rectangle(); 27 | 28 | r1->l = 5; 29 | r1->b = 10; 30 | cout << "The Area of the rectangle is " << r1->area() << endl; 31 | cout << "The Perimeter of the rectangle is " << r1->perimeter() << endl; 32 | 33 | } -------------------------------------------------------------------------------- /src/cpp/02 OOPS concept/07 this function.cpp: -------------------------------------------------------------------------------- 1 | // inline fxn will call itself with the fxn else it is called outside and take memory outside we will use just inline keyword 2 | // let us understand this function 3 | 4 | #include 5 | using namespace std; 6 | 7 | class Rectangle 8 | { 9 | private : 10 | int length; 11 | int breadth; 12 | public: 13 | Rectangle(int length, int breadth) // here we need use to this fxn inored to show that private data is assigned to this parameter 14 | { 15 | this->length = length; // it removes the ambiguity between the parameters of the class 16 | this->breadth = breadth; 17 | } 18 | 19 | int area() 20 | { 21 | return length * breadth; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | Rectangle r(1, 5); 28 | cout << r.area() << endl; 29 | return 0; 30 | } -------------------------------------------------------------------------------- /src/cpp/02 OOPS concept/08 Struct & Class.cpp: -------------------------------------------------------------------------------- 1 | // The difference btw struct and class 2 | // this struct is more like class 3 | // in struct everything by defalut public whereas in class everything is by default private this is only the difference 4 | #include 5 | using namespace std; 6 | 7 | struct Demo // we can keep class on the place of struct by making the data member public 8 | { 9 | int x; 10 | int y; 11 | 12 | void Display() 13 | { 14 | cout << x << " " << y << endl; 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | Demo d; 21 | d.x = 20; 22 | d.y = 10; 23 | d.Display(); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /src/cpp/02 OOPS concept/09 Students details.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Student 5 | { 6 | private : 7 | string name; 8 | int roll; 9 | float maths; 10 | float eng; 11 | float science; 12 | public: 13 | Student(int r, string n, int m, int e, int s) 14 | { 15 | roll = r; 16 | name = n; 17 | maths = m; 18 | eng = e; 19 | science = s; 20 | } 21 | 22 | float Totalmarks() 23 | { 24 | return maths + eng + science; 25 | } 26 | char Grade() 27 | { 28 | float avg = Totalmarks() / 3; 29 | if (avg > 60) 30 | return 'A'; 31 | else if (avg >= 40 && avg < 60) 32 | return 'B'; 33 | else 34 | return 'C'; 35 | 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | int roll; 42 | string name; 43 | float m, e, s; 44 | cout << "Enter the roll number of the student " << endl; 45 | cin >> roll; 46 | cout << "Enter the name of the students " << endl; 47 | cin >> name; 48 | cout << "Enter the marks in the 3 subjects " << endl; 49 | cin >> m >> e >> s; 50 | Student s1(roll, name, m, e, s); 51 | cout << "Total marks of the student is " << s1.Totalmarks() << endl; 52 | cout << "Grade of the student is " << s1.Grade() << endl; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /src/cpp/02 OOPS concept/10 class and obj.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Employee{ 5 | public: 6 | string Name; 7 | string Company; 8 | int Age; 9 | 10 | void IntroduceYourself(){ 11 | cout <<"Name - "< 2 | using namespace std; 3 | 4 | class Employee{ 5 | public: 6 | string Name; 7 | string Company; 8 | int Age; 9 | 10 | void IntroduceYourself(){ 11 | cout <<"Name - "< 2 | using namespace std; 3 | 4 | int main(){ 5 | // for (int i = 0; i < 40; i++) 6 | // { 7 | // /* code */ 8 | // if(i==2){ 9 | // break; 10 | // } 11 | // cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | // What is a pointer? ----> Data type which holds the address of other data types 6 | int a=3; 7 | int* b; 8 | b = &a; 9 | 10 | // & ---> (Address of) Operator 11 | cout<<"The address of a is "<<&a< (value at) Dereference operator 15 | cout<<"The value at address b is "<<*b< 2 | using namespace std; 3 | 4 | int main(){ 5 | // Array Example 6 | int marks[] = {23, 45, 56, 89}; 7 | 8 | int mathMarks[4]; 9 | mathMarks[0] = 2278; 10 | mathMarks[1] = 738; 11 | mathMarks[2] = 378; 12 | mathMarks[3] = 578; 13 | 14 | cout<<"These are math marks"< 2 | using namespace std; 3 | 4 | typedef struct employee 5 | { 6 | /* data */ 7 | int eId; //4 8 | char favChar; //1 9 | float salary; //4 10 | } ep; 11 | 12 | union money 13 | { 14 | /* data */ 15 | int rice; //4 16 | char car; //1 17 | float pounds; //4 18 | }; 19 | 20 | 21 | int main(){ 22 | enum Meal{ breakfast, lunch, dinner}; 23 | Meal m1 = lunch; 24 | cout<<(m1==2); 25 | // cout< 2 | using namespace std; 3 | 4 | // Function prototype 5 | // type function-name (arguments); 6 | // int sum(int a, int b); //--> Acceptable 7 | // int sum(int a, b); //--> Not Acceptable 8 | int sum(int, int); //--> Acceptable 9 | // void g(void); //--> Acceptable 10 | void g(); //--> Acceptable 11 | 12 | int main(){ 13 | int num1, num2; 14 | cout<<"Enter first number"<>num1; 16 | cout<<"Enter second number"<>num2; 18 | // num1 and num2 are actual parameters 19 | cout<<"The sum is "< 2 | using namespace std; 3 | 4 | int sum(int a, int b){ 5 | int c = a + b; 6 | return c; 7 | } 8 | 9 | // This will not swap a and b 10 | void swap(int a, int b){ //temp a b 11 | int temp = a; //4 4 5 12 | a = b; //4 5 5 13 | b = temp; //4 5 4 14 | } 15 | 16 | // Call by reference using pointers 17 | void swapPointer(int* a, int* b){ //temp a b 18 | int temp = *a; //4 4 5 19 | *a = *b; //4 5 5 20 | *b = temp; //4 5 4 21 | } 22 | 23 | // Call by reference using C++ reference Variables 24 | // int & 25 | void swapReferenceVar(int &a, int &b){ //temp a b 26 | int temp = a; //4 4 5 27 | a = b; //4 5 5 28 | b = temp; //4 5 4 29 | // return a; 30 | } 31 | 32 | int main(){ 33 | int x =4, y=5; 34 | // cout<<"The sum of 4 and 5 is "< 2 | using namespace std; 3 | 4 | int fib(int n){ 5 | if(n<2){ 6 | return 1; 7 | } 8 | return fib(n-2) + fib(n-1); 9 | } 10 | 11 | // fib(5) 12 | // fib(4) + fib(3) 13 | // fib(2) + fib(3) + fib(2) + fib(3) 14 | 15 | int factorial(int n){ 16 | if (n<=1){ 17 | return 1; 18 | } 19 | return n * factorial(n-1); 20 | } 21 | 22 | // Step by step calculation of factorial(4) 23 | // factorial(4) = 4 * factorial(3); 24 | // factorial(4) = 4 * 3 * factorial(2); 25 | // factorial(4) = 4 * 3 * 2 * factorial(1); 26 | // factorial(4) = 4 * 3 * 2 * 1; 27 | // factorial(4) = 24; 28 | 29 | int main(){ 30 | // Factorial of a number: 31 | // 6! = 6*5*4*3*2*1 = 720 32 | // 0! = 1 by definition 33 | // 1! = 1 by definition 34 | // n! = n * (n-1)! 35 | int a; 36 | cout<<"Enter a number"<>a; 38 | // cout<<"The factorial of "< 2 | using namespace std; 3 | 4 | int sum(float a, int b){ 5 | cout<<"Using function with 2 arguments"< 2 | using namespace std; 3 | 4 | class Shop 5 | { 6 | int itemId[100]; 7 | int itemPrice[100]; 8 | int counter; 9 | 10 | public: 11 | void initCounter(void) { counter = 0; } 12 | void setPrice(void); 13 | void displayPrice(void); 14 | }; 15 | 16 | void Shop ::setPrice(void) 17 | { 18 | cout << "Enter Id of your item no " << counter + 1 << endl; 19 | cin >> itemId[counter]; 20 | cout << "Enter Price of your item" << endl; 21 | cin >> itemPrice[counter]; 22 | counter++; 23 | } 24 | 25 | void Shop ::displayPrice(void) 26 | { 27 | for (int i = 0; i < counter; i++) 28 | { 29 | cout << "The Price of item with Id " << itemId[i] << " is " << itemPrice[i] << endl; 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | Shop dukaan; 36 | dukaan.initCounter(); 37 | dukaan.setPrice(); 38 | dukaan.setPrice(); 39 | dukaan.setPrice(); 40 | dukaan.displayPrice(); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /src/cpp/18 Array of Objects & Passing Objects as Function Arguments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class complex{ 5 | int a; 6 | int b; 7 | 8 | public: 9 | void setData(int v1, int v2){ 10 | a = v1; 11 | b = v2; 12 | } 13 | 14 | void setDataBySum(complex o1, complex o2){ 15 | a = o1.a + o2.a; 16 | b = o1.b + o2.b; 17 | } 18 | 19 | void printNumber(){ 20 | cout<<"Your complex number is "< 2 | using namespace std; 3 | class c2; 4 | 5 | class c1{ 6 | int val1; 7 | friend void exchange(c1 & , c2 &); 8 | public: 9 | void indata(int a){ 10 | val1 = a; 11 | } 12 | 13 | void display(void){ 14 | cout<< val1 < 2 | using namespace std; 3 | 4 | class Complex 5 | { 6 | int a, b; 7 | 8 | public: 9 | // Creating a Constructor 10 | // Constructor is a special member function with the same name as of the class. 11 | //It is used to initialize the objects of its class 12 | //It is automatically invoked whenever an object is created 13 | 14 | Complex(void); // Constructor declaration 15 | 16 | void printNumber() 17 | { 18 | cout << "Your number is " << a << " + " << b << "i" << endl; 19 | } 20 | }; 21 | 22 | Complex ::Complex(void) // ----> This is a default constructor as it takes no parameters 23 | { 24 | a = 0; 25 | b = 0; 26 | // cout<<"Hello world"; 27 | } 28 | 29 | int main() 30 | { 31 | Complex c1, c2, c3; 32 | c1.printNumber(); 33 | c2.printNumber(); 34 | c3.printNumber(); 35 | 36 | return 0; 37 | } 38 | 39 | 40 | /* Characteristics of Constructors 41 | 42 | 1. It should be declared in the public section of the class 43 | 2. They are automatically invoked whenever the object is created 44 | 3. They cannot return values and do not have return types 45 | 4. It can have default arguments 46 | 5. We cannot refer to their address 47 | 48 | */ 49 | -------------------------------------------------------------------------------- /src/cpp/23 Paramererized Constructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Complex 6 | { 7 | int a, b; 8 | 9 | public: 10 | Complex(int, int); // Constructor declaration 11 | 12 | void printNumber() 13 | { 14 | cout << "Your number is " << a << " + " << b << "i" << endl; 15 | } 16 | }; 17 | 18 | Complex ::Complex(int x, int y) // ----> This is a parameterized constructor as it takes 2 parameters 19 | { 20 | a = x; 21 | b = y; 22 | // cout<<"Hello world"; 23 | } 24 | 25 | int main(){ 26 | // Implicit call 27 | Complex a(4, 6); 28 | a.printNumber(); 29 | 30 | // Explicit call 31 | Complex b = Complex(5, 7); 32 | b.printNumber(); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /src/cpp/24 Parameterized Constructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Point{ 5 | int x, y; 6 | public: 7 | Point(int a, int b){ 8 | x = a; 9 | y = b; 10 | } 11 | 12 | void displayPoint(){ 13 | cout<<"The point is ("< 2 | using namespace std; 3 | 4 | class Complex 5 | { 6 | int a, b; 7 | 8 | public: 9 | Complex(){ 10 | a = 0; 11 | b =0; 12 | } 13 | 14 | Complex(int x, int y) 15 | { 16 | a = x; 17 | b = y; 18 | } 19 | 20 | Complex(int x){ 21 | a = x; 22 | b = 0; 23 | } 24 | 25 | 26 | 27 | void printNumber() 28 | { 29 | cout << "Your number is " << a << " + " << b << "i" << endl; 30 | } 31 | }; 32 | int main() 33 | { 34 | Complex c1(4, 6); 35 | c1.printNumber(); 36 | 37 | Complex c2(5); 38 | c2.printNumber(); 39 | 40 | Complex c3; 41 | c3.printNumber(); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /src/cpp/26 Constructor with dafults Arguments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Simple{ 5 | int data1; 6 | int data2; 7 | int data3; 8 | 9 | public: 10 | Simple(int a, int b=9, int c=8){ 11 | data1 = a; 12 | data2 = b; 13 | data3 = c; 14 | } 15 | 16 | void printData(); 17 | 18 | }; 19 | 20 | void Simple :: printData(){ 21 | cout<<"The value of data1, data2 and data3 is "< 2 | using namespace std; 3 | 4 | 5 | class Number{ 6 | int a; 7 | public: 8 | Number(){ 9 | a = 0; 10 | } 11 | 12 | Number(int num){ 13 | a = num; 14 | } 15 | // When no copy constructor is found, compiler supplies its own copy constructor 16 | Number(Number &obj){ 17 | cout<<"Copy constructor called!!!"< 2 | using namespace std; 3 | 4 | // Destructor never takes an argument nor does it return any value 5 | int count=0; 6 | 7 | class num{ 8 | public: 9 | num(){ 10 | count++; 11 | cout<<"This is the time when constructor is called for object number"< 2 | using namespace std; 3 | 4 | // Base Class 5 | class Employee 6 | { 7 | public: 8 | int id; 9 | float salary; 10 | Employee(int inpId) 11 | { 12 | id = inpId; 13 | salary = 34.0; 14 | } 15 | Employee() {} 16 | }; 17 | 18 | // Derived Class syntax 19 | /* 20 | class {{derived-class-name}} : {{visibility-mode}} {{base-class-name}} 21 | { 22 | class members/methods/etc... 23 | } 24 | Note: 25 | 1. Default visibility mode is private 26 | 2. Public Visibility Mode: Public members of the base class becomes Public members of the derived class 27 | 3. Private Visibility Mode: Public members of the base class becomes Private members of the derived class 28 | 4. Private members are never inherited 29 | */ 30 | 31 | // Creating a Programmer class derived from Employee Base class 32 | class Programmer : public Employee 33 | { 34 | public: 35 | int languageCode; 36 | Programmer(int inpId) 37 | { 38 | id = inpId; 39 | languageCode = 9; 40 | } 41 | void getData(){ 42 | cout< 2 | using namespace std; 3 | 4 | class Base 5 | { 6 | int data1; // private by default and is not inheritable 7 | public: 8 | int data2; 9 | void setData(); 10 | int getData1(); 11 | int getData2(); 12 | }; 13 | 14 | void Base ::setData(void) 15 | { 16 | data1 = 10; 17 | data2 = 20; 18 | } 19 | 20 | int Base::getData1() 21 | { 22 | return data1; 23 | } 24 | 25 | int Base::getData2() 26 | { 27 | return data2; 28 | } 29 | 30 | class Derived : public Base 31 | { // Class is being derived publically 32 | int data3; 33 | 34 | public: 35 | void process(); 36 | void display(); 37 | }; 38 | 39 | void Derived ::process() 40 | { 41 | data3 = data2 * getData1(); 42 | } 43 | 44 | void Derived ::display() 45 | { 46 | cout << "Value of data 1 is " << getData1() << endl; 47 | cout << "Value of data 2 is " << data2 << endl; 48 | cout << "Value of data 3 is " << data3 << endl; 49 | } 50 | 51 | int main() 52 | { 53 | Derived der; 54 | der.setData(); 55 | der.process(); 56 | der.display(); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /src/cpp/32 Protected Access Modifier.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Base{ 5 | protected: 6 | int a; 7 | private: 8 | int b; 9 | 10 | }; 11 | /* 12 | For a protected member: 13 | Public derivation Private Derivation Protected Derivation 14 | 1. Private members Not Inherited Not Inherited Not Inherited 15 | 2. Protected members Protected Private Protected 16 | 3. Public members Public Private Protected 17 | */ 18 | class Derived: protected Base{ 19 | 20 | }; 21 | 22 | int main(){ 23 | Base b; 24 | Derived d; 25 | // cout< 2 | using namespace std; 3 | 4 | class Base1{ 5 | public: 6 | void greet(){ 7 | cout<<"How are you?"< 2 | using namespace std; 3 | /* 4 | Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list 5 | followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class. 6 | 7 | 8 | Syntax for initialization list in constructor: 9 | constructor (argument-list) : initilization-section 10 | { 11 | assignment + other code; 12 | } 13 | 14 | class Test{ 15 | int a; 16 | int b; 17 | public: 18 | Test(int i, int j) : a(i), b(j){constructor-body} 19 | }; 20 | 21 | */ 22 | class Test 23 | { 24 | int a; 25 | int b; 26 | 27 | public: 28 | // Test(int i, int j) : a(i), b(j) 29 | // Test(int i, int j) : a(i), b(i+j) 30 | // Test(int i, int j) : a(i), b(2 * j) 31 | // Test(int i, int j) : a(i), b(a + j) 32 | // Test(int i, int j) : b(j), a(i+b) -->RED Flag this will create problems because a will be initialized first 33 | Test(int i, int j) 34 | { 35 | a = i; 36 | b = j; 37 | cout << "Constructor executed"< 12 | using namespace std; 13 | 14 | int main(){ 15 | // Basic Example 16 | int a = 4; 17 | int* ptr = &a; 18 | *ptr = 999; 19 | cout<<"The value of a is "<<*(ptr)< 2 | using namespace std; 3 | 4 | class ShopItem 5 | { 6 | int id; 7 | float price; 8 | public: 9 | void setData(int a, float b){ 10 | id = a; 11 | price = b; 12 | } 13 | void getData(void){ 14 | cout<<"Code of this item is "<< id<>p>>q; 40 | // (*ptr).setData(p, q); 41 | ptr->setData(p, q); 42 | ptr++; 43 | } 44 | 45 | for (i = 0; i < size; i++) 46 | { 47 | cout<<"Item number: "<getData(); 49 | ptrTemp++; 50 | } 51 | 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /src/cpp/43 this Pointer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. 3 | Therefore, inside a member function, this may be used to refer to the invoking object. 4 | 5 | Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer. 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | class A{ 11 | int a; 12 | public: 13 | // A & setData(int a){ 14 | void setData(int a){ 15 | this->a = a; 16 | // return *this; 17 | } 18 | 19 | void getData(){ 20 | cout<<"The value of a is "< 2 | using namespace std; 3 | 4 | // code resuablity // avoid fxn overloading // once written and use it many times 5 | template 6 | T mymax(T a, T b) 7 | { 8 | return (a > b) ? a : b; // if a is greater then b print a else b // it is ternary operator it encapusulates if-else-return in one line 9 | } 10 | int main() { 11 | cout << mymax(9, 10) << endl; 12 | cout << mymax(20.3, 6.7) << endl; 13 | cout << mymax('E', 'A')< 8 | using namespace std; 9 | template 10 | class Array { 11 | private : 12 | T* ptr; 13 | int size; 14 | public: 15 | Array(T arr[], int s); 16 | void print(); 17 | }; 18 | 19 | template 20 | Array :: Array(T arr[], int s) 21 | { 22 | int i; 23 | ptr = new T[s]; 24 | size = s; 25 | for (i = 0; i < size; i++) 26 | ptr[i] = arr[i]; 27 | } 28 | template 29 | void Array ::print() 30 | { 31 | int i; 32 | for (i = 0; i < size; i++) 33 | cout <<" " << *(ptr + i); 34 | cout <> no; 42 | cout << "Enter all the elements of array " << endl; 43 | for (int i = 0; i < no; i++) 44 | cin >> arr[i]; 45 | Array a(arr, no); 46 | a.print(); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /src/cpp/STL/03 Bubble sort using template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | void bubblesort(T a[], int n) 5 | { 6 | for (int i = 0; i < n - 1; i++) 7 | for (int j = n - 1; i < j; j--) 8 | if (a[j] < a[j - 1]) 9 | swap(a[j], a[j - 1]); 10 | } 11 | int main() 12 | { 13 | int a[5] = { 10,25,63,8,9 }; 14 | int n = sizeof(a) / sizeof(a[0]); 15 | bubblesort(a, n); 16 | cout << "sorted array" << endl; 17 | for (int i = 0; i < n; i++) 18 | cout << a[i] << " "; 19 | cout << endl; 20 | return 0; 21 | 22 | } -------------------------------------------------------------------------------- /src/cpp/STL/05 print string in lexicographical order.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | /*given N string, print unique sting 6 | in Lexicpgraphical order with their frequency 7 | 8 | */ 9 | 10 | // map uses red black self balencing tree in backend and it always stored keys in sorted manner 11 | int main () 12 | { 13 | mapm; // key as string and value as integer 14 | int n; 15 | cin>>n; 16 | for (int i=0;i>s; 20 | /*m[s]=m[s]+1; or we can write m[s]++ both are same */ 21 | m[s]++; 22 | } 23 | for (auto pr : m) 24 | { 25 | cout < 16 | //#include 17 | //using namespace std; 18 | //int main() 19 | //{ 20 | // vector my_vect{ 1,5,9,8,7 }; 21 | // for (auto i = my_vect.begin(); i != my_vect.end(); ++i) 22 | // cout << ' ' << *i; 23 | // 24 | // return 0; 25 | //} 26 | 27 | 28 | //------------------------------------------------------------------------------------------------------------------------------------- 29 | 30 | //another example for string 31 | 32 | #include 33 | #include 34 | #include 35 | using namespace std; 36 | int main() 37 | { 38 | // declaration of vector in container 39 | vector my_vect { "My","Name","Is SHIVENDRA" }; 40 | // here we are using begin and end to print the full string 41 | for (auto i = my_vect.begin(); i != my_vect.end(); ++i) 42 | cout << ' ' << *i; 43 | return 0; 44 | } -------------------------------------------------------------------------------- /src/cpp/STL/Containers/Vector/02 end fun.cpp: -------------------------------------------------------------------------------- 1 | /*- Vector is same like dynamic array 2 | - when we are going to del or insert any val it is going to resize by itself 3 | - that is their storage being handalled automatically by containers 4 | */ 5 | /* 6 | * end () is also used in above program so its syntax is similar to begin () 7 | * end () return an iterators pointing to next to last elements 8 | * vectorname.end() 9 | Parameters : 10 | No parameters are passed. 11 | Returns : 12 | This function returns a bidirectional 13 | iterator pointing to next to last element. 14 | 15 | 16 | */ 17 | 18 | //#include 19 | //#include 20 | //using namespace std; 21 | //int main() 22 | //{ 23 | // vector my_vect{ 1,5,9,8,7 }; 24 | // for (auto i = my_vect.begin(); i != my_vect.end(); ++i) 25 | // cout << ' ' << *i; 26 | // 27 | // return 0; 28 | //} 29 | 30 | 31 | //------------------------------------------------------------------------------------------------------------------------------------- 32 | 33 | //another example for string 34 | 35 | #include 36 | #include 37 | #include 38 | using namespace std; 39 | int main() 40 | { 41 | // declaration of vector in container 42 | vector my_vect { "My","Name","Is SHIVENDRA" }; 43 | // here we are using begin and end to print the full string 44 | for (auto i = my_vect.begin(); i != my_vect.end(); ++i) 45 | cout << ' ' << *i; 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /src/cpp/STL/Containers/Vector/03 Maps creation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main () 4 | { 5 | mapm; 6 | // all the keys of map will always be unique 7 | // map stores keys & values in sorted order 8 | // if we keep string then it will be stored in lexographical order 9 | 10 | m[8]="Shivendra"; // insertion takes O(logn) 11 | m[3]="abc"; // O(logm) 12 | m[5]="cdc"; // when we wrote this even after there is no value it will take any value by it self 13 | m.insert({1,"shiv"}); 14 | /*map:: iterator it; 15 | for (it=m.begin();it!=m.end();++it) 16 | { 17 | cout<first<<" "<second<<" "< 2 | using namespace std; 3 | 4 | // Lets make a function to print map and getting its size 5 | 6 | void print(map &m) 7 | { 8 | cout<<"Size of the map is "<m; 16 | // all the keys of map will always be unique 17 | // map stores keys & values in sorted order 18 | // if we keep string then it will be stored in lexographical order 19 | 20 | m[8]="Shivendra"; // insertion takes O(logn) 21 | m[3]="abc"; // O(logm) 22 | m[5]="cdc"; // when we wrote this even after there is no value it will take any value by it self 23 | m.insert({1,"shiv"}); 24 | print(m); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/cpp/STL/Containers/Vector/05 map.find, map.erase, m.clear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Lets make a function to print map and getting its size 5 | 6 | void print(map &m) 7 | { 8 | cout<<"Size of the map is "<m; 16 | // map can store any kind of data types even set, vector .. 17 | // all the keys of map will always be unique 18 | // map stores keys & values in sorted order 19 | // if we keep string then it will be stored in lexographical order 20 | 21 | m[8]="Shivendra"; // insertion takes O(logn) 22 | m[3]="abc"; // O(logm) 23 | m[5]="cdc"; // when we wrote this even after there is no value it will take any value by it self 24 | m.insert({1,"shiv"}); 25 | // when you want to directly access some specific value in map using m[key] or m.find(key), these are log(n) operations as log(n)time is taken by map to search this key. 26 | m.erase(3); // O(log(n)) 27 | auto it = m.find(3); // O(long(n)) 28 | m.erase(it); // O(log(n)) // it will delete the key 3 and value 29 | m.clear(); // it will clear(delete) full map 30 | if (it== m.end()) 31 | cout<<"NO Value "; 32 | else 33 | cout <first <<" "<second <<" "< 2 | using namespace std; 3 | 4 | // Lets make a function to print map and getting its size 5 | 6 | void print(unordered_map &m) 7 | { 8 | cout<<"Size of the map is "< Insertion and access 17 | // 3. valid keys datatypes : we cant keep all type of data types like pair of pair 18 | // we can use int double string float ... cz these all have possible hash value 19 | unordered_mapm; 20 | // unordered map does not store in sorted manner it will print randomly 21 | 22 | m[8]="Shivendra"; // insertion takes O(1) 23 | m[3]="abc"; 24 | m[5]="cdc"; 25 | m.insert({1,"shiv"}); 26 | /*map:: iterator it; 27 | for (it=m.begin();it!=m.end();++it) 28 | { 29 | cout<first<<" "<second<<" "<