├── 1.c++ ├── ARIMA MODELLING.ipynb ├── BFS.c ├── Binary Search ├── binary_search.cpp ├── first_and_last_occurance.cpp ├── sq_root_using_binary_search.cpp └── wheel.cpp ├── Binary Tree ├── Binary_Tree_BFS_Traversal-I.cpp ├── Binary_Tree_Inorder_and_Postorder_Traversal.cpp ├── Binary_Tree_Level_Order_Print_Recursive.cpp └── Binary_Tree_Preorder_Build_and_Print.cpp ├── Bit Manipulation └── Unique_no1.cpp ├── Bucketsort.cpp ├── C++ STL ├── Algorithms STL │ ├── Activity_Selection_Problem.cpp │ ├── binary_search.cpp │ ├── bubble_sort_with_comparator.cpp │ ├── money_change_problem.cpp │ ├── next_permutation_problem.cpp │ ├── pair_class.cpp │ ├── rotate_and_next_permutation.cpp │ ├── search.cpp │ └── sort.cpp ├── Coding Questions STL │ ├── Find_Upper_and_Lower_bound.cpp │ ├── Find_the_greater_element.cpp │ ├── Form_Biggest_Number.cpp │ ├── Highest_frequency( Hashing).cpp │ ├── Merge_K_sorted_Arrays.cpp │ ├── Pair_of_Roses.cpp │ ├── Prateek_Sir_and_Coding.cpp │ ├── Strongest_Fighter.cpp │ ├── Tricky_Permutations.cpp │ ├── sort_game.cpp │ └── sring_sort.cpp ├── Generic Programming │ ├── Linear_Search.cpp │ ├── Linear_Search_Using_Iterator.cpp │ ├── Linear_Search_Using_Iterator_and_comparator.cpp │ └── largest_number.cpp ├── List STL │ └── list.cpp ├── Map STL │ ├── Vertical_Order_Print.cpp │ ├── map.cpp │ └── multimap.cpp ├── Priority Queue STL │ ├── 1.jpeg │ ├── 2.jpeg │ ├── 3.jpeg │ ├── 4.jpeg │ ├── functor.cpp │ ├── largest_k_elements.cpp │ ├── priority_queue.cpp │ ├── priority_queue_compartor_class.cpp │ └── queue.cpp ├── Set STL │ ├── Set_example.cpp │ ├── Unique_permutations.cpp │ └── set.cpp ├── String STL │ ├── String_Class.cpp │ ├── sorting.cpp │ └── strtok_function.cpp ├── Unordered Maps STL │ ├── Unordered_Map_Hash_Function_ for_Custom_Class.cpp │ ├── phonebook.cpp │ ├── trie_map.cpp │ └── unordered_map.cpp └── Vectors STL │ ├── car_sorting_prob_using_vector_and_class.cpp │ └── car_sorting_prob_using_vector_and_pair.cpp ├── Challenges └── sorting_in_linear_time.cpp4.cpp ├── ConnenctedComponent.cpp ├── Count and Toggle Queries on a Binary Array.cpp ├── CountingSort.cpp ├── DFS ├── DP ├── LCS.cpp └── alphacode.cpp ├── DSA ├── 2D Arrays │ ├── 2D_Arrays.cpp │ ├── spiral_print.cpp │ └── wave_print.cpp ├── Algorithms STL │ ├── binary_search.cpp │ ├── find_function.cpp │ └── rotate_and_next_permutation.cpp ├── Arrays │ ├── Generate_all_subarrays.cpp │ ├── Linear_Search.cpp │ ├── Maximum_subarray_sum1.cpp │ ├── Maximum_subarray_sum2.cpp │ ├── Maximum_subarray_sum3(Kadane's_Algo).cpp │ ├── Pair_Sum_Problem.cpp │ ├── Radix Sort │ ├── Remove duplicate elements from sorted array.cpp │ ├── Smallest_Missing_No.cpp │ ├── Smallest_and_Largest_number.cpp │ ├── binary_search.cpp │ ├── bubble_sort.cpp │ ├── bubble_sort_with_comparator.cpp │ ├── inbuilt_sort.cpp │ ├── insertion_sort.cpp │ ├── maxindex.cpp │ ├── pair_sum(two_pointer_approach).cpp │ └── selection_sort.cpp ├── Memory Allocation │ ├── 2D_dynamic_array.cpp │ └── Memory_allocation.cpp └── Strings │ ├── Tokenization_using_strtok.cpp │ └── string_class.cpp ├── Detect Cycle ├── Detect_cycle_directed_graph.c++ ├── Find_First_and_last_position_of_element_in_sorted_array.cpp ├── HeapSort.cpp ├── HuffmanCoding.cpp ├── InsertionSort.cpp ├── Knapsack.cpp ├── LUCKYNUM.cpp ├── Leetcode Important questions ├── LinkedList │ ├── LLtoBST.cpp │ ├── LinkedListCycle1.cpp │ ├── LinkedListCycle2.cpp │ ├── RemoveDuplicatesinLL.cpp │ ├── RemoveNthNodeFromEndofLL.cpp │ ├── mergetwoSortedLL.cpp │ └── swapNodeinPairs.cpp └── array │ ├── 3sum.cpp │ ├── 4sum.cpp │ ├── DuplicateNum.cpp │ ├── RemoveDuplicateSortedArray.cpp │ ├── buySellStock.cpp │ ├── findallduplicate.cpp │ ├── majorityelement.cpp │ ├── mergesort.cpp │ ├── minchocolates.cpp │ ├── minchocolates.exe │ ├── movezeros.cpp │ ├── removeduplicateinstring.cpp │ ├── sortcolors.cpp │ ├── spiralmatrix.cpp │ ├── strStr.cpp │ ├── twoSum.cpp │ ├── valid_parenthesis.cpp │ └── waterContainer.cpp ├── LinearSearch.cpp ├── Linked List ├── 01.Display a linked list.c ├── 02.Create and Display a linked list.cpp ├── 03.Recursive display of linked list.c ├── 04.Count and sum.c ├── 05.Printing the maximum element.c ├── 06.Display,count,max,sum.c ├── 07.Searching an element.c ├── 08.Searching an element using Recursion.c ├── 09.Insertion.cpp ├── 10.Circular linked list.c ├── 11.Insert in sorted linked list.c ├── 12.Delete in linked list.c ├── 13.Removing the duplicate.c ├── Even Odd Segregation of Linked List ├── LinkedList.cpp ├── LinkedList1.cpp ├── LinkedList2.cpp ├── LinkedList3.cpp ├── LinkedList4.cpp ├── LinkedList5.cpp ├── LinkedList6.cpp ├── LinkedList7.cpp ├── Linkedlist Palindrome ├── SeggregateOddEven.cpp └── circular_linked_list.cpp ├── LockingAndUnlockingOfResources.cpp ├── LongestCommonPrefix.cpp ├── MergeSort.cpp ├── Merge_Sorted_Array.cpp ├── N-Queen.cpp ├── Practice Problems ├── Arrays_Sum_Of_Two_Arrays.cpp ├── Container_with_most_water.cpp ├── Find_First_and_last_position_of_element_in_sorted_array.cpp ├── Median_of_two_sorted_arrays.cpp ├── Merge_Sorted_Array.cpp ├── PallindromeNumber.cpp ├── Search_in_rotated_sorted_array.cpp ├── Search_insert_position.cpp ├── Sort_Colors.cpp ├── Squares_of_a_Sorted_Array.cpp ├── consecutive_characters.cpp ├── next_permutation.cpp ├── remove_duplicates_from_sorted_array.cpp └── remove_element.cpp ├── Queues ├── Queue_Implementation_Queue_using_Array.cpp └── Queue_Implementation_Queue_using_Linked_List_STL.cpp ├── QuickSrtRandmPivot.c ├── README.md ├── Rabinkarp.cpp ├── RadixSort.cpp ├── Recurssion ├── Array_is_sorted.cpp ├── factorial.cpp ├── fibonacci.cpp ├── first_occurance__linear_search.cpp ├── increasing_decreasing.cpp └── last_occurance.cpp ├── Reverse word.cpp ├── Reverse_Stack_Using_1_Extra_Stack.cpp ├── SelectionSort ├── ShellSort.cpp ├── Space Time Complexity Analysis └── Bubble_Sort_Time_Experiment.cpp ├── Squares_of_a_Sorted_Array.cpp ├── StackImplimentationclass.cpp ├── Stacks ├── Reverse_Stack_Using_1_Extra_Stack.cpp ├── reverse O(1).cpp ├── stack_implementation_using_vector.cpp ├── stack_implementation_using_vector_template.cpp └── stack_stl.cpp ├── TernarySearch.cpp ├── Vectors ├── car_sorting_prob_using_vector_and_class.cpp └── car_sorting_prob_using_vector_and_pair.cpp ├── adjacency_list.c++ ├── array deletion ├── arrayInsertionUsingLinkedList ├── bellmanford algorithm ├── bipartiteGraphDfs.cpp ├── buzznumber.java ├── calculator_tk.py ├── catalan_numbers.cpp ├── comb_sort.cpp ├── dateTimeMonthCal.cpp ├── detect_a_loop_inLinkedList.cpp ├── dfs.c++ ├── dijkstra ├── doublyylinkedlist.c ├── factorial.cpp ├── factorialOfBigNumber.class ├── factorialOfBigNumber.java ├── fibonacci.c ├── file tree.py ├── firstrepeatingarrayelement.java ├── floyd warshall algorithm ├── ford fulkerson.cpp ├── heap sort ├── hexadecimaltobinary.cpp ├── iteratorHashmap.cpp ├── kasai_Algorithm.cpp ├── kmp_lps.c++ ├── kruskal algorithm ├── last_occurance.cpp ├── main.py ├── prim's algorithm ├── priority Queue ├── HeapUse.cpp ├── HeapUse.exe ├── MaxheapinbuiltPQ.cpp ├── MinheapinbuiltPQ.cpp ├── MinheapinbuiltPQ.exe ├── inbuiltPQ.exe ├── ksmallestelement.cpp ├── ksmallestelement.exe ├── ksortedarray.cpp ├── ksortedarray.exe ├── maxheapsort.cpp ├── minheapusingArray.cpp └── minheapusingArray.exe ├── removeduplicate.cpp ├── rockPaperScissor.cpp ├── scape_goat_tree.cpp ├── sorting_in_linear_time.cpp4.cpp ├── stringduplicate.cpp ├── substringHashing.cpp ├── towerofhanoi.cpp ├── trie.cpp ├── tsp.cpp └── vertical_sum_a_tree.cpp /1.c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int x=10; 7 | int y=2; 8 | cout<<(x=x+y)< 2 | using namespace std; 3 | 4 | float square_root(int n, int p){ 5 | int start=0; 6 | int end=n; 7 | float ans=-1; 8 | while(start<=end){ 9 | int mid=(start+end)/2; 10 | if(mid*mid==n){ 11 | return mid; 12 | } 13 | if(mid*mid>n){ 14 | end=mid-1; 15 | } 16 | else{ 17 | ans=mid; 18 | start=mid+1; 19 | } 20 | } 21 | 22 | //We found integer part using binary search 23 | //For floating part we will be using brute force approach 24 | 25 | float inc=0.1; 26 | for(int times=1;times<=p;times++){ 27 | while(ans*ans<=n){ 28 | ans=ans+inc; 29 | } 30 | //overshot the value 31 | ans=ans-inc; 32 | inc=inc/10; 33 | } 34 | 35 | return ans; 36 | } 37 | 38 | 39 | int main(){ 40 | 41 | int n; 42 | cin>>n; 43 | 44 | cout< 2 | using namespace std; 3 | 4 | int first_occurance(int a[], int n, int key){ 5 | int start=0; 6 | int end=n-1; 7 | int ans=-1; 8 | //Update in direction that ans value gets lower and lower 9 | while(start<=end){ 10 | int mid=(start+end)/2; 11 | if(a[mid]==key){ 12 | ans=mid; 13 | end=mid-1; 14 | } 15 | else if(a[mid]>key){ 16 | end=mid-1; 17 | } 18 | else{ 19 | start=mid+1; 20 | } 21 | } 22 | return ans; 23 | } 24 | 25 | int last_occurance(int a[], int n, int key){ 26 | int start=0; 27 | int end=n-1; 28 | int ans=-1; 29 | while(start<=end){ 30 | int mid=(start+end)/2; 31 | if(a[mid]==key){ 32 | // Do two things 33 | ans=mid; 34 | start=mid+1; //Do not stop but explore the right part of the arrays 35 | } 36 | else if(a[mid]>key){ 37 | end=mid-1; 38 | } 39 | else{ 40 | start=mid+1; 41 | } 42 | } 43 | return ans; 44 | } 45 | 46 | int main(){ 47 | 48 | int n, key; 49 | /* 50 | cout<<"Enter size of array"<>n; 52 | 53 | int a[1000]; 54 | 55 | cout<<"Enter elements of array"<>a[i]; 58 | }*/ 59 | 60 | int a[]={1,2,5,8,8,8,8,8,10,12,15,20}; 61 | n=sizeof(a)/sizeof(int); 62 | 63 | cout<<"Enter key to search"<>key; 65 | 66 | cout< 2 | using namespace std; 3 | 4 | float square_root(int n, int p){ 5 | int start=0; 6 | int end=n; 7 | float ans=-1; 8 | while(start<=end){ 9 | int mid=(start+end)/2; 10 | if(mid*mid==n){ 11 | return mid; 12 | } 13 | if(mid*mid>n){ 14 | end=mid-1; 15 | } 16 | else{ 17 | ans=mid; 18 | start=mid+1; 19 | } 20 | } 21 | 22 | //We found integer part using binary search 23 | //For floating part we will be using brute force approach 24 | 25 | float inc=0.1; 26 | for(int times=1;times<=p;times++){ 27 | while(ans*ans<=n){ 28 | ans=ans+inc; 29 | } 30 | //overshot the value 31 | ans=ans-inc; 32 | inc=inc/10; 33 | } 34 | 35 | return ans; 36 | } 37 | 38 | 39 | int main(){ 40 | 41 | int n; 42 | cin>>n; 43 | 44 | cout< 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*left; 8 | node*right; 9 | 10 | node(int d){ 11 | data = d; 12 | left = NULL; 13 | right = NULL; 14 | } 15 | }; 16 | 17 | node* buildTree(){ 18 | int d; 19 | cin>>d; 20 | 21 | if(d==-1){ 22 | return NULL; 23 | } 24 | node * root = new node(d); 25 | root->left = buildTree(); 26 | root->right = buildTree(); 27 | return root; 28 | } 29 | void print(node *root){ 30 | if(root==NULL){ 31 | return; 32 | } 33 | //Otherwise, print root first followed by subtrees(children) 34 | cout<data<<" "; 35 | print(root->left); 36 | print(root->right); 37 | } 38 | 39 | void printIn(node *root){ 40 | if(root==NULL){ 41 | return; 42 | } 43 | //Otherwise, left root right 44 | printIn(root->left); 45 | cout<data<<" "; 46 | printIn(root->right); 47 | } 48 | 49 | void printPost(node *root){ 50 | if(root==NULL){ 51 | return; 52 | } 53 | 54 | printPost(root->left); 55 | printPost(root->right); 56 | cout<data<<" "; 57 | } 58 | 59 | 60 | int main(){ 61 | node* root = buildTree(); 62 | print(root); 63 | cout< 78 | 79 | 8 10 1 -1 -1 6 9 -1 -1 7 -1 -1 3 -1 14 13 -1 -1 -1 80 | 81 | output -> 82 | 83 | 8 10 1 6 9 7 3 14 13 84 | 1 10 9 6 7 8 3 13 14 85 | 1 9 7 6 10 13 14 3 8 86 | 87 | */ 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /Binary Tree/Binary_Tree_Preorder_Build_and_Print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*left; 8 | node*right; 9 | 10 | node(int d){ 11 | data = d; 12 | left = NULL; 13 | right = NULL; 14 | } 15 | }; 16 | 17 | node* buildTree(){ 18 | int d; 19 | cin>>d; 20 | 21 | if(d==-1){ 22 | return NULL; 23 | } 24 | node * root = new node(d); 25 | root->left = buildTree(); 26 | root->right = buildTree(); 27 | return root; 28 | } 29 | void print(node *root){ 30 | if(root==NULL){ 31 | return; 32 | } 33 | //Otherwise, print root first followed by subtrees(children) 34 | cout<data<<" "; 35 | print(root->left); 36 | print(root->right); 37 | } 38 | int main(){ 39 | node* root = buildTree(); 40 | print(root); 41 | 42 | 43 | return 0; 44 | } 45 | 46 | /* 47 | 48 | Input -> 49 | 50 | 3 4 -1 6 -1 -1 5 1 -1 -1 -1 51 | 52 | output -> 53 | 54 | 3 4 6 5 1 55 | 56 | */ 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /Bit Manipulation/Unique_no1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Problem - Unique Number 2N + 1 5 | //Given a list of numbers where every numbers occur twice except one, find that unique number 6 | 7 | int main() { 8 | int n; 9 | cin>>n; 10 | 11 | int no; 12 | int ans=0; 13 | //Bitwise XOR operator to solve 14 | //Helped to solve without using any extra storage/memory 15 | for(int i=0;i>no; 17 | ans=ans^no; 18 | } 19 | cout< 2 | using namespace std; 3 | 4 | /* 5 | 6 | You are given n activities (from 0 to n-1) with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. 7 | 8 | Input Format 9 | The first line consists of an integer T, the number of test cases. For each test case, the first line consists of an integer N, the number of activities. Then the next N lines contain two integers m and n, the start and end time of each activity. 10 | 11 | Constraints 12 | 1<=t<=50 13 | 1<=n<=10000 14 | 1<=A[i]<=100 15 | 16 | Output Format 17 | For each test case find the maximum number of activities that you can do. 18 | 19 | 20 | Sample Input 21 | 1 22 | 3 23 | 10 20 24 | 12 15 25 | 20 30 26 | 27 | Sample Output 28 | 2 29 | Explanation 30 | Person can only do 0th and 2nd activities. 31 | 32 | */ 33 | 34 | bool compare(paira , pairb){ 35 | return a.second>t; 42 | while(t--){ 43 | int n, count=0; 44 | cin>>n; 45 | vector> v; 46 | for(int i=0;i>x>>y; 49 | v.push_back({x,y}); 50 | } 51 | 52 | sort(v.begin(),v.end(),compare); 53 | int i=0; 54 | for(auto it:v){ 55 | if(it.first>=i){ 56 | count++; 57 | i=it.second; 58 | } 59 | } 60 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[]={20,30,40,40,40,50,100,1100}; 6 | int n=sizeof(arr)/sizeof(int); 7 | int key; 8 | cin>>key; 9 | 10 | bool present = binary_search(arr,arr+n,key); 11 | 12 | if(present){ 13 | cout<<"present"<=key) 31 | upper_bound will give the first element that is strictly greater than key (>key) 32 | */ 33 | -------------------------------------------------------------------------------- /C++ STL/Algorithms STL/bubble_sort_with_comparator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool compare(int a, int b){ 5 | return a>b; 6 | } 7 | 8 | // If a>b then it will act as sort in descending order and if a>n; 24 | 25 | int a[1000]; 26 | 27 | for(int i=0;i>a[i]; 29 | } 30 | 31 | //Sort an array using sort() function, it is more efficient than other sorting function like merge sort, bubble sort etc. 32 | bubble_sort(a,n,compare); 33 | 34 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool compare(int a, int b){ 5 | return a<=b; 6 | } 7 | 8 | int main(){ 9 | //Indian money change 10 | int coins[]={1,2,5,10,20,50,100,200,500,2000}; 11 | int n=sizeof(coins)/sizeof(int); 12 | int money; 13 | cin>>money; 14 | 15 | while(money>0){ 16 | int lb=lower_bound(coins,coins+n,money,compare)-coins-1; 17 | int m=coins[lb]; 18 | cout< 2 | using namespace std; 3 | /* 4 | 5 | Given an array Arr[], Treat each element of the array as the digit and whole array as the number. Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers. 6 | 7 | If such arrangement is not possible, it must be rearranged as the lowest possible order ie, sorted in an ascending order. 8 | 9 | Note: The replacement must be in-place, do not allocate extra memory. 10 | 11 | */ 12 | 13 | 14 | int main(){ 15 | 16 | int t; 17 | cin>>t; 18 | while(t--){ 19 | int n; 20 | cin>>n; 21 | int arr[n]; 22 | for(int i=0;i>arr[i]; 24 | } 25 | next_permutation(arr,arr+n); 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | // Pair 7 | pair p; 8 | p.first=10; 9 | p.second='A'; 10 | 11 | // Another way 12 | pair p2(p); 13 | cout< p3=make_pair(100,"Audi"); 18 | cout<>a>>b; 23 | 24 | pair p4=make_pair(a,b); 25 | // We can also make array of pairs or vector of pairs 26 | 27 | // pair inside another pair 28 | pair,string> car; 29 | car.second="Audi"; 30 | car.first.first=100; 31 | car.first.second=200; 32 | 33 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int arr[]={10,20,30,40,50}; 7 | int n=sizeof(arr)/sizeof(int); 8 | rotate(arr,arr+2,arr+n); 9 | for(int i=0;i v{10,20,30,40,50}; 18 | rotate(v.begin(),v.begin()+3,v.end()); 19 | for(int i=0;i v1{1,2,3}; 25 | next_permutation(v1.begin(),v1.end()); 26 | next_permutation(v1.begin(),v1.end()); 27 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int arr[]={2,5,6,11,15,8}; 6 | int n=sizeof(arr)/sizeof(int); 7 | int key; 8 | cin>>key; 9 | auto it=find(arr,arr+n,key); 10 | int index=it-arr; 11 | if(index==n){ 12 | cout< 2 | using namespace std; 3 | 4 | bool compare(int a, int b){ 5 | return a>b; 6 | } 7 | 8 | // If a>b then it will act as sort in descending order and if a>n; 13 | 14 | int a[1000]; 15 | 16 | for(int i=0;i>a[i]; 18 | } 19 | 20 | //Sort an array using sort() function, it is more efficient than other sorting function like merge sort, bubble sort etc. 21 | sort(a,a+n,compare); 22 | 23 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* 5 | Find position of the last and first occurrence of a given number in a sorted array. If number does not exist then print lower and upper bound as -1. 6 | 7 | Input Format-> 8 | First line contains an integer n denoting the size of the array. 9 | Second line contains n space separated integers denoting array elements. 10 | Third line contains single integer Q denoting the no of queries. 11 | Q lines follow , each containing a single integer x that is to be searched in the array. 12 | 13 | Constraints-> 14 | 1 <= N <= 10^5 15 | 1 <= Q <= 100 16 | 17 | Output Format-> 18 | For each query , print the lowerbound and the upperbound for the number x in a new line each. 19 | 20 | Sample Input-> 21 | 5 22 | 1 2 3 3 4 23 | 3 24 | 2 25 | 3 26 | 10 27 | Sample Output-> 28 | 1 1 29 | 2 3 30 | -1 -1 31 | Explanation-> 32 | The first and the last occurrence of 2 in the given array are both 1. 33 | The first occurrence of 3 is at index=2 and last at index=3. 34 | 10 is not present in the array so we print -1 for it. 35 | 36 | */ 37 | 38 | int main(){ 39 | int n; 40 | cin>>n; 41 | int arr[n]; 42 | for(int i=0;i>arr[i]; 45 | } 46 | int q; 47 | cin>>q; 48 | for(int i=0;i>query; 51 | bool present = binary_search(arr,arr+n,query); 52 | if(!present){ 53 | cout<<"-1 -1"< 2 | using namespace std; 3 | 4 | /* 5 | 6 | We are given a circular array, print the next greater number for every element. If it is not found print -1 for that number. To find the next greater number for element Ai , start from index i + 1 and go uptil the last index after which we start looking for the greater number from the starting index of the array since array is circular. 7 | 8 | Input Format-> 9 | First line contains the length of the array n. Second line contains the n space separated integers. 10 | 11 | Constraints-> 12 | 1 <= n <= 10^6 13 | -10^8 <= Ai <= 10^8 , 0<= i< n 14 | 15 | Output Format-> 16 | Print n space separated integers each representing the next greater element. 17 | 18 | Sample Input-> 19 | 3 20 | 1 2 3 21 | Sample Output-> 22 | 2 3 -1 23 | Explanation-> 24 | Next greater element for 1 is 2, 25 | for 2 is 3 but not present for 3 therefore -1 26 | 27 | */ 28 | 29 | int main(){ 30 | 31 | int n; 32 | cin>>n; 33 | int v[2*n]; 34 | int v2[2*n]; 35 | for(int i=0;i>v[i]; 38 | //v.push_back(val); 39 | } 40 | 41 | for(int i=n;i<2*n;i++){ 42 | v[i]=v[i-n]; 43 | } 44 | 45 | stack s; 46 | 47 | 48 | for(int i=2*n-1;i>=0;i--){ 49 | while(!s.empty()&&s.top()<=v[i]){ 50 | s.pop(); 51 | } 52 | if(s.empty()){ 53 | v2[i]=-1; 54 | } 55 | else{ 56 | v2[i]=s.top(); 57 | } 58 | s.push(v[i]); 59 | } 60 | 61 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool compare(string a, string b){ 5 | string ab=a.append(b); 6 | string ba=b.append(a); 7 | return ab>ba; 8 | } 9 | 10 | 11 | int main(){ 12 | int t; 13 | cin>>t; 14 | while(t--){ 15 | int n; 16 | cin>>n; 17 | vector v; 18 | 19 | for(int i=0;i>val; 22 | v.push_back(val); 23 | } 24 | 25 | sort(v.begin(),v.end(),compare); 26 | 27 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* 5 | Given an array find the number which comes with maximum frequency. It must work in O(n) time complexity. 6 | 7 | Input Format-> 8 | Enter the size of the array N and add N more numbers and store in an array 9 | 10 | Constraints-> 11 | 1 <= N <= 10^7 12 | -109 <= A[i] <= 109 13 | 14 | Output Format-> 15 | Display the number with the maximum frequency. 16 | 17 | Sample Input-> 18 | 5 19 | 1 2 2 2 3 20 | Sample Output-> 21 | 2 22 | Explanation-> 23 | 2 has the highest frequency in the array i.e. 3. 24 | 25 | */ 26 | int main(){ 27 | 28 | int n, count=1, count2=1, res; 29 | cin>>n; 30 | vector v; 31 | for(int i=0;i>val; 34 | v.push_back(val); 35 | } 36 | 37 | sort(v.begin(),v.end()); 38 | res=v[0]; 39 | 40 | for(int i=1;icount2){ 46 | count2=count; 47 | res=v[i-1]; 48 | } 49 | count=1; 50 | } 51 | } 52 | if(count>count2){ 53 | count2=count; 54 | res=v[n-1]; 55 | } 56 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | int n,k; 7 | cin>>n>>k; 8 | vector v; 9 | 10 | for(int i=0;i>val; 14 | v.push_back(val); 15 | } 16 | } 17 | 18 | sort(v.begin(),v.end()); 19 | 20 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | /* 9 | Prateek sir loves coding. Hence, he took up position of an instructor and founded Coding Blocks, a startup that serves students with awesome code modules. It is a very famous place and students are always queuing up to have one of those modules. Each module has a cost associated with it. The modules are kept as a pile. The job of an instructor is very difficult. He needs to handle two types of queries: 10 | 11 | 1) Student Query: When a student demands a module, the code module on the top of the pile is given and the student is charged according to the cost of the module. This reduces the height of the pile by 1. In case the pile is empty, the student goes away empty-handed. 12 | 13 | 2) Instructor Query: Sir prepares a code module and adds it on top of the pile. And reports the cost of the module. Help him manage this process. 14 | 15 | Input Format 16 | First line contains an integer Q, the number of queries. Q lines follow. A Type-1 ( Student ) Query, is indicated by a single integer 1 in the line. A Type-2 ( Instructor ) Query, is indicated by two space separated integers 2 and C (cost of the module prepared) . 17 | 18 | Constraints 19 | Q < 10^5 20 | 21 | Output Format 22 | For each Type-1 Query, output the price that student has to pay i.e. cost of the module given to the customer in a new line. If the pile is empty, print "No Code" (without the quotes). 23 | 24 | */ 25 | 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | int x,p; 31 | stack s2; 32 | for(int i=0;i>x; 34 | if(x!=1){ 35 | cin>>p; 36 | s2.push(p); 37 | } 38 | else if(!s2.empty()){ 39 | cout< 2 | using namespace std; 3 | 4 | /* 5 | 6 | There is a group of MMA fighters standing together in a line. Given the value of their strengths, find the strength of the strongest fighter in continuous sub-groups of size k. 7 | 8 | Input Format-> 9 | First line contains an integer N, the number of fighters Followed by N integers where i'th integer denotes the strength of i'th fighter. Next line contains the size of sub-group k 10 | 11 | Constraints-> 12 | 1<=N<=10^7 13 | 1<=k<=N 14 | 1 <= Ai <= 100000 15 | 16 | Output Format-> 17 | Space separated integers in a single line denoting strength of strongest fighters in the groups. 18 | 19 | Sample Input-> 20 | 5 21 | 1 3 1 4 5 22 | 3 23 | Sample Output-> 24 | 3 4 5 25 | Explanation-> 26 | First sub-group: 1 3 1 --> Max strength is 3 27 | Second sub-group: 3 1 4 --> Max strength is 4 28 | Third sub-group: 1 4 5 --> Max strength is 5 29 | 30 | 31 | */ 32 | 33 | int main() 34 | { 35 | int n; 36 | cin >> n; 37 | vector v; 38 | for(int i=0;i>val; 41 | v.push_back(val); 42 | } 43 | 44 | int k; 45 | cin>>k; 46 | 47 | deque dq; 48 | 49 | for(int i=0;iv[dq.back()]){ 51 | dq.pop_back(); 52 | } 53 | dq.push_back(i); 54 | } 55 | 56 | for(int i=k;i=v[dq.back()]){ 64 | dq.pop_back(); 65 | } 66 | dq.push_back(i); 67 | } 68 | 69 | cout< 2 | using namespace std; 3 | 4 | /* 5 | 6 | Given a string containing duplicates, print all its distinct permutations such that there are no duplicate permutations and all permutations are printed in a lexicographic order. 7 | 8 | Input Format-> 9 | The first and only line of the test case contains the input string. 10 | 11 | Constraints-> 12 | Length of the string <= 8 13 | 14 | Output Format-> 15 | Print all the distinct permutations in a lexicographic order such that each permutation is in a new line. Note that there should not be any duplicate permutations. 16 | 17 | Sample Input-> 18 | ABA 19 | Sample Output-> 20 | AAB 21 | ABA 22 | BAA 23 | Explanation-> 24 | The possible permutations for the given string are { "AAB" , "AAB" , "ABA" , "BAA" } . We skip the repeating "AAB" permutation and only print it in once. Also we print the final output in lexicographical order. 25 | 26 | 27 | */ 28 | 29 | void permute(string str,int i,int &c,string ans[]) 30 | { 31 | if(i==str.size()) 32 | { 33 | ans[c++] = str; 34 | return ; 35 | } 36 | 37 | for(int j=i;j>str; 50 | 51 | string ans[100000]; 52 | int c=0; 53 | 54 | permute(str,0,c,ans); 55 | sort(ans,ans+c); 56 | 57 | string temp=""; 58 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | /* 8 | 9 | Sanju needs your help! He gets a list of employees with their salary. The maximum salary is 100. 10 | 11 | Sanju is supposed to arrange the list in such a manner that the list is sorted in decreasing order of salary. And if two employees have the same salary, they should be arranged in lexicographical manner such that the list contains only names of those employees having salary greater than or equal to a given number x. 12 | 13 | Help Sanju prepare the same! 14 | 15 | Input Format 16 | On the first line of the standard input, there is an integer x. The next line contans integer N, denoting the number of employees. N lines follow, which contain a string and an integer, denoting the name of the employee and his salary. 17 | 18 | */ 19 | 20 | 21 | class emp{ 22 | public: 23 | string name; 24 | int sal; 25 | emp(){ 26 | 27 | } 28 | emp(string n,int s){ 29 | name = n; 30 | sal = s; 31 | } 32 | }; 33 | 34 | class compare{ 35 | public: 36 | bool operator()(emp A,emp B){ 37 | if(A.sal==B.sal){ 38 | return A.name > B.name; 39 | } 40 | return A.sal < B.sal; 41 | } 42 | }; 43 | 44 | 45 | int main() 46 | { 47 | 48 | priority_queue, compare> l; 49 | priority_queue, compare> l1; 50 | int x, n; 51 | cin>>x>>n; 52 | for(int i=0;i>name>>sal; 56 | emp e(name,sal); 57 | l.push(e); 58 | } 59 | 60 | for(int i=0;i=x){ 64 | emp e1(e.name,e.sal); 65 | l1.push(e1); 66 | } 67 | l.pop(); 68 | } 69 | int k=l1.size(); 70 | 71 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | /* 9 | 10 | Nishant is a very naughty boy in Launchpad Batch. 11 | One day he was playing with strings, and randomly shuffled them all. 12 | Your task is to help Nishant Sort all the strings ( lexicographically ) 13 | but if a string is present completely as a prefix in another string, then string with longer length should come first. 14 | Eg bat, batman are 2 strings and the string bat is present as a prefix in Batman - then sorted order should have - Batman, bat. 15 | 16 | 17 | */ 18 | 19 | bool compare(string a, string b){ 20 | if(a==b.substr(0,a.length()) || b==a.substr(0,b.length())) 21 | return a.length()>b.length(); 22 | return a>n; 29 | vector v; 30 | for(int i=0;i>name; 33 | v.push_back(name); 34 | } 35 | 36 | sort(v.begin(),v.end(),compare); 37 | 38 | 39 | for(int i=0;i 2 | using namespace std; 3 | 4 | //Linear Search 5 | 6 | // Using Template we get freedom from all types of datatypes 7 | template 8 | 9 | int search(T a[], int n, T key) 10 | { 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* Adventage of Learning Iterators */ 5 | //Linear Search 6 | 7 | 8 | // Templates + Iterators 9 | template 10 | //Now it works on different containers like vector, list etc. 11 | ForwardIterator search(ForwardIterator start, ForwardIterator end, T key){ 12 | while(start!=end){ 13 | if(*start==key){ 14 | return start; 15 | } 16 | start ++; 17 | } 18 | return end; 19 | } 20 | 21 | int main() { 22 | list l; 23 | l.push_back(1); 24 | l.push_back(2); 25 | l.push_back(5); 26 | l.push_back(3); 27 | 28 | auto it=search(l.begin(),l.end(),5); 29 | 30 | if(it==l.end()){ 31 | cout<<"element not present"< :: iterator 42 | and ForwardIterator will have this(list :: iterator) data type*/ 43 | 44 | 45 | -------------------------------------------------------------------------------- /C++ STL/Generic Programming/Linear_Search_Using_Iterator_and_comparator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* Adventage of Learning Iterators */ 5 | //Linear Search 6 | 7 | // Templates + Iterators + Comparators 8 | template 9 | //Now it works on different containers like vector, list etc. 10 | ForwardIterator search(ForwardIterator start, ForwardIterator end, T key, compare cmp){ 11 | while(start!=end){ 12 | if(cmp(*start,key)){ 13 | return start; 14 | } 15 | start ++; 16 | } 17 | return end; 18 | } 19 | 20 | /* The above is how STL classes/functions are implemented and Generic function is written */ 21 | 22 | class Book{ 23 | public: 24 | string name; 25 | int price; 26 | 27 | Book(){ 28 | 29 | } 30 | 31 | Book(string name, int price){ 32 | this->name = name; 33 | this->price = price; 34 | } 35 | }; 36 | 37 | class BookCompare{ 38 | 39 | public: 40 | bool operator()(Book A, Book B){ 41 | if(A.name==B.name){ 42 | return true; 43 | } 44 | return false; 45 | } 46 | }; 47 | 48 | int main() { 49 | 50 | Book b1("C++",100); // Old Edition 51 | Book b2("python",120); 52 | Book b3("Java",130); 53 | 54 | vector l; 55 | l.push_back(b1); 56 | l.push_back(b2); 57 | l.push_back(b3); 58 | 59 | Book booktofind("C++",110); //New Edition 60 | BookCompare cmp; 61 | 62 | auto it=search(l.begin(),l.end(),booktofind,cmp); 63 | 64 | if(it!=l.end()){ 65 | cout<<"Book Found"< :: iterator 76 | and ForwardIterator will have this(list :: iterator) data type*/ 77 | 78 | 79 | /* Templetes give us the freedom from underlying data type 80 | Iterators give us the freedom from containers 81 | Comparators give us the freedom from different types of operations operating on the data 82 | */ 83 | 84 | -------------------------------------------------------------------------------- /C++ STL/Generic Programming/largest_number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a list of non negative integers, arrange them such that they form the largest number. 3 | For example: 4 | Given [3, 30, 34, 5, 9], the largest formed number is 9534330. 5 | */ 6 | 7 | 8 | /* 9 | Solution Approach 10 | Sorting all numbers in descending order is the simplest solution that occurs to us. But this doesn’t work. 11 | For example, 548 is greater than 60, but in the output, 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in the output. 12 | The solution is to use any comparison based sorting algorithm. Thus, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers. 13 | Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). 14 | If XY is larger, then, in the output, X should come before Y, else Y should come before X. 15 | For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put Y first. 16 | */ 17 | #include 18 | 19 | bool comp(string a,string b) 20 | { 21 | string ab=a.append(b); 22 | string ba=b.append(a); 23 | return ab.compare(ba)>0?true:false; 24 | } 25 | string Solution::largestNumber(const vector &A) { 26 | vector Ans; 27 | string s=""; 28 | int i,c=0; 29 | for(i=0;i 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | list l; 8 | 9 | //Init 10 | list l1{1,2,3,10,8,5}; 11 | //Different Datatype 12 | list l2{"apple","guava","mango","banana"}; 13 | l2.push_back("pineapple"); 14 | 15 | //sort 16 | l2.sort(); 17 | 18 | //reverse 19 | l2.reverse(); 20 | 21 | // pop_front 22 | cout< "; 33 | } 34 | cout<"; 43 | } 44 | cout<>f; 48 | //l2.remove(f); 49 | for(string s:l2){ 50 | cout<"; 51 | } 52 | cout<"; 62 | } 63 | cout<"; 72 | } 73 | 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /C++ STL/Map STL/Vertical_Order_Print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /* 7 | Vertical order print of a binary tree 8 | Q-> Given a Binary tree, we need to print it in vertical order 9 | 10 | */ 11 | 12 | class node{ 13 | public: 14 | int data; 15 | node*left; 16 | node*right; 17 | 18 | node(int d){ 19 | data = d; 20 | left = right = NULL; 21 | } 22 | }; 23 | //pass it by reference 24 | void verticalOrderPrint(node*root, int d,map > &m){ 25 | //base case 26 | if(root==NULL){ 27 | return; 28 | } 29 | //otherwise 30 | m[d].push_back(root->data); 31 | verticalOrderPrint(root->left,d-1,m); 32 | verticalOrderPrint(root->right,d+1,m); 33 | return; 34 | } 35 | 36 | int main(){ 37 | 38 | node*root = new node(1); 39 | root->left = new node(2); 40 | root->right = new node(3); 41 | root->left->left = new node(4); 42 | root->left->right = new node(5); 43 | 44 | root->right->left = new node(6); 45 | root->right->right = new node(7); 46 | 47 | root->right->right->right = new node(9); 48 | 49 | root->left->right->right = new node(8); 50 | 51 | map > m; 52 | int d = 0; 53 | verticalOrderPrint(root,d,m); 54 | 55 | for(auto p:m){ 56 | // cout<"; 57 | for(int x:p.second){ 58 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /* 7 | Mutimap is similar to map except it contains elements where multiple keys can have same element 8 | 9 | 10 | In maps and multipmaps - insert, find & erase work in log(n) time because of self balancing BST 11 | 12 | */ 13 | 14 | int main() { 15 | multimap m; 16 | int n; 17 | cin>>n; 18 | 19 | for(int i=0;i>ch>>s; 23 | m.insert(make_pair(ch,s)); 24 | } 25 | 26 | auto it = m.begin(); 27 | m.erase(it); // this will erase first element 28 | 29 | auto it2 = m.lower_bound('b'); //batman 30 | auto it3 = m.upper_bound('d'); //elephant 31 | for(auto it4=it2;it4!=it3;it4++){ 32 | cout<second<<" ,"<second=="cat"){ 37 | m.erase(f); 38 | } 39 | 40 | //Try to print the entire map 41 | for(auto p:m){ 42 | cout< "< 48 | 7 49 | b batman 50 | a apple 51 | b boat 52 | a angry 53 | c cat 54 | d dog 55 | e elephant 56 | 57 | */ 58 | -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankit-kaushal/Lets-go-Code/18df118198e2f43bf0964195837cee2e6a42983d/C++ STL/Priority Queue STL/1.jpeg -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankit-kaushal/Lets-go-Code/18df118198e2f43bf0964195837cee2e6a42983d/C++ STL/Priority Queue STL/2.jpeg -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/3.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankit-kaushal/Lets-go-Code/18df118198e2f43bf0964195837cee2e6a42983d/C++ STL/Priority Queue STL/3.jpeg -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/4.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ankit-kaushal/Lets-go-Code/18df118198e2f43bf0964195837cee2e6a42983d/C++ STL/Priority Queue STL/4.jpeg -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/functor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Fun{ 6 | public: 7 | void operator()(string s){ 8 | cout<<"Having Fun with "< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | //To find(print) largest k numbers when in the input there is -1 8 | 9 | void query_print(priority_queue,greater> pq){ 10 | //We will print the heap by first copying the heap and then prining and do dop on the copy heap 11 | while(!pq.empty()){ 12 | cout<,greater> pq; 21 | 22 | int cs=0; 23 | int k=3; 24 | 25 | while(scanf("%d",&no)!=EOF){ //This statement to take input in continious stream 26 | if(no==-1){ 27 | query_print(pq); 28 | } 29 | else if(cspq.top()){ 35 | pq.pop(); 36 | pq.push(no); 37 | } 38 | } 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | /* 45 | Input-> 46 | 1 2 3 4 -1 5 9 10 -1 12 15 8 6 -1 47 | Output-> 48 | 2,3,4, 49 | 5,9,10, 50 | 10,12,15, 51 | 52 | */ 53 | -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/priority_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | priority_queue q; //This type of priority queue id based on MAX HEAP, where we get maximum element first 8 | //We can convert it to MIN HEAP by passing inbuilt comparator i.e, greater 9 | // priority_queue, greater> q; 10 | 11 | for(int i=1;i<=5;i++){ 12 | q.push(i); 13 | } 14 | 15 | while(!q.empty()){ 16 | cout<< q.top() <<" <-"; 17 | q.pop(); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/priority_queue_compartor_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | class Person{ 8 | public: 9 | string name; 10 | int age; 11 | 12 | Person(){ 13 | 14 | } 15 | Person(string n,int a){ 16 | name = n; 17 | age = a; 18 | } 19 | }; 20 | 21 | class PersonCompare{ 22 | public: 23 | bool operator()(Person A,Person B){ 24 | //cout<<"Comparing "<>n; 35 | 36 | priority_queue, PersonCompare > pq; 37 | 38 | for(int i=0;i>name>>age; 42 | Person p(name,age); 43 | pq.push(p); 44 | } 45 | 46 | //find top 3 oldest person 47 | 48 | int k = 3; 49 | for(int i=0;i 60 | 5 61 | A 20 62 | B 3 63 | C 40 64 | D 10 65 | E 15 66 | 67 | Output-> 68 | C 40 69 | A 20 70 | E 15 71 | 72 | 73 | */ 74 | -------------------------------------------------------------------------------- /C++ STL/Priority Queue STL/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | queue q; 8 | 9 | for(int i=1;i<=5;i++){ 10 | q.push(i); 11 | } 12 | 13 | while(!q.empty()){ 14 | cout<< q.front() <<" <-"; 15 | q.pop(); 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C++ STL/Set STL/Set_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | set> s; 8 | 9 | s.insert(make_pair(10,1)); 10 | s.insert(make_pair(10,5)); 11 | s.insert(make_pair(20,1)); 12 | s.insert(make_pair(20,100)); 13 | s.insert(make_pair(5,1)); 14 | s.insert(make_pair(5,3)); 15 | 16 | 17 | s.erase(s.find(make_pair(5,3))); 18 | s.insert(make_pair(5,4)); 19 | 20 | auto it=s.lower_bound(make_pair(20,1)); 21 | cout<first<<","<second<first<<","<second< Then we can put pair as (10,INT_MAX). Here, INT_MAX is infinity*/ 27 | 28 | it=s.upper_bound(make_pair(10,INT_MAX)); 29 | cout<first<<","<second< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /* 7 | Question is finding permutation of a string 8 | 9 | Suppose we want to get unique permutation and we want to get permutayion in lexicographic sorted order 10 | So, we can generate permutations and we can store all of them in a set data structure 11 | */ 12 | 13 | void permute(char a[],int i,set &s){ 14 | if(a[i]=='\0'){ 15 | //cout<>a; 32 | set s; 33 | permute(a,0,s); 34 | 35 | //loop over the s 36 | for(auto str:s){ 37 | cout< 2 | #include 3 | using namespace std; 4 | 5 | /* 6 | Set is a container that is used to store a unique collection of elements. By default set is ordered. 7 | But, we also have unordered set. Set follows criteria to sort the elements by their values. 8 | Set uses tree like data structure. It can be a BST, it can be a Red Black Tree. 9 | Once inserted element in set, we can't update it. To update, we need to remove that element and insert the updated one. 10 | 11 | */ 12 | 13 | 14 | int main() { 15 | 16 | int arr[]={10,20,11,9,8,11,10}; 17 | int n=sizeof(arr)/sizeof(int); 18 | 19 | set s; 20 | for(int i=0;i::iterator it=s.begin();it!=s.end();it++){ 36 | cout<<*(it)<<","; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /C++ STL/String STL/String_Class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | // String Init 6 | string s0; 7 | string s1("Hello"); 8 | string s2="Hello World"; 9 | 10 | string s3(s2); 11 | string s4=s3; 12 | 13 | char a[]={'a','b','c','\0'}; 14 | string s5(a); 15 | 16 | cout<0 or <0 43 | cout<s0){ 48 | cout<<"Mango is lexicographically greater than apple"< 2 | using namespace std; 3 | 4 | // Here we define our own sorting criteria so that it sorts out length wise 5 | bool compare(string a,string b){ 6 | //if both have equal length that sort lexicographically 7 | if(a.length()==b.length()){ 8 | return ab.length(); 11 | } 12 | 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | cin.get(); // To consume any extra enter that is after \n 17 | 18 | string s[100]; // Here vector of Strings can also be used in place of array of Strings 19 | 20 | for(int i=0;i 2 | using namespace std; 3 | 4 | //char *strtok(char*s,char *delimiters) 5 | //we use it to break a string into a list of tokens 6 | //returns a token on each subsequent call 7 | //on the first call function should be passed with string argument for 's' 8 | //on subsequent calls we should pass the string argument as null 9 | 10 | int main(){ 11 | 12 | char s[100]= "Today is a rainy day"; 13 | 14 | 15 | // Delimeter with " " (space) 16 | char *ptr = strtok(s," "); 17 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | 9 | unordered_map > phonebook; 10 | 11 | phonebook["rahul"].push_back("9110"); 12 | phonebook["rahul"].push_back("9112"); 13 | phonebook["rahul"].push_back("9113"); 14 | phonebook["rahul"].push_back("9114"); 15 | phonebook["kajal"].push_back("8112"); 16 | phonebook["kajal"].push_back("8113"); 17 | phonebook["kajal"].push_back("8114"); 18 | 19 | for(auto p:phonebook){ 20 | cout<<"Name" <"; 21 | for(string s:p.second){ 22 | cout<>name; 29 | 30 | if(phonebook.count(name)==0){ 31 | cout<<"Absent "; 32 | } 33 | else{ 34 | for(string s:phonebook[name]){ 35 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /* 7 | Unordered Map is an implementation of HashTable. 8 | 9 | Best thing about unordered map is, we can do insertion, deletion or search. All of these work on O(1) on Average. 10 | On average because there may be change that our table grows bigger and we have to do rehashing. In that case this time is O(n). 11 | */ 12 | 13 | int main() { 14 | 15 | unordered_map m; 16 | 17 | //1. Insert 18 | m.insert(make_pair("Mango",100)); 19 | 20 | pair p; 21 | p.first = "Apple"; 22 | p.second = 120; 23 | 24 | m.insert(p); 25 | 26 | m["Banana"] = 20; 27 | 28 | //2. Search 29 | string fruit; 30 | cin>>fruit; 31 | 32 | //update the price 33 | m[fruit] += 22; 34 | 35 | auto it = m.find(fruit); 36 | 37 | if(it!=m.end()){ 38 | cout<<"price of "<first<<" and "<second< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Car{ 8 | public: 9 | string car_name; 10 | int x,y; 11 | Car(){ 12 | 13 | } 14 | Car(string n, int x, int y){ 15 | car_name=n; 16 | this->x=x; 17 | this->y=y; 18 | } 19 | int distance(){ 20 | //returns square of distance from origin 21 | return x*x+y*y; 22 | } 23 | }; 24 | 25 | bool compare(Car A, Car B){ 26 | int da=A.distance(); 27 | int db=B.distance(); 28 | if(da==db){ 29 | return A.car_name.length()>n; 37 | string name; 38 | vector v; 39 | for(int i=0;i>name>>x>>y; 41 | Car temp(name,x,y); 42 | v.push_back(temp); 43 | } 44 | 45 | sort(v.begin(),v.end(),compare); 46 | 47 | for(auto c:v){ 48 | cout<<"Car Name: "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool compare(pair p1, pair p2){ 7 | int d1=p1.first*p1.first+p1.second*p1.second; 8 | int d2=p2.first*p2.first+p2.second*p2.second; 9 | 10 | //If two cars have same distance then sort according to x coordinate 11 | if(d1==d2){ 12 | return p1.first>n; 21 | 22 | vector> v; 23 | for(int i=0;i>x>>y; 25 | v.push_back(make_pair(x,y)); 26 | } 27 | 28 | sort(v.begin(),v.end(),compare); 29 | 30 | for(auto p:v){ 31 | cout<<"Car "< 2 | using namespace std; 3 | 4 | /* 5 | You will be given an array containing only 0s, 1s and 2s. 6 | You have sort the array in linear time that is O(N) where N is the size of the array. 7 | 8 | */ 9 | 10 | void linear_time(int a[], int n){ 11 | int low=0, mid=0, high=n-1; 12 | while(mid<=high){ 13 | if(a[mid]==0){ 14 | swap(a[mid],a[low]); 15 | mid++; 16 | low++; 17 | } 18 | else if(a[mid]==1){ 19 | mid++; 20 | } 21 | else{ 22 | swap(a[mid],a[high]); 23 | high--; 24 | } 25 | } 26 | } 27 | 28 | int main(){ 29 | int n; 30 | cin>>n; 31 | int a[n]; 32 | for(int i=0;i>a[i]; 34 | } 35 | linear_time(a,n); 36 | for(int i=0;i 2 | using namespace std; 3 | vector v[100000+1]; 4 | vector visit(100000+1,false); 5 | void dfs(int vertex) 6 | { 7 | visit[vertex]=true; 8 | cout<>vertex>>edge; 22 | while(edge--) 23 | { 24 | int a,b; 25 | cin>>a>>b; 26 | v[a].push_back(b); 27 | v[b].push_back(a); 28 | } 29 | for(int i=1;i<=vertex;i++) 30 | { 31 | if(visit[i]==false) 32 | { 33 | count++; 34 | dfs(i); 35 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int a,b; 6 | cout<<"enter the length and maximum value\n"; 7 | cin>>a>>b; 8 | int k1[a], k2[a]; 9 | for(int i=0;i>k1[i]; 12 | 13 | } 14 | int l1[b+1]; // creating an extra array for counting frequency of elements 15 | for(int i=0;i=0; i--) 26 | { 27 | k2[l1[k1[i]]]=k1[i]; // puting elements in sorted order 28 | l1[k1[i]]-=1; 29 | } 30 | for(int i=1; i<=a;i++) // printing array 31 | cout< 2 | using namespace std; 3 | 4 | int lcshelpermemo(char *s1,char *s2,int m1,int m2,int **dp) 5 | { 6 | int ans=0; 7 | if(m1==0 || m2==0) 8 | return 0; 9 | if(dp[m1][m2]!=-1) 10 | { 11 | return dp[m1][m2]; 12 | } 13 | if(s1[0]==s2[0]) 14 | { 15 | ans =1+lcshelpermemo(s1+1,s2+1,m1-1,m2-1,dp); 16 | } 17 | else{ 18 | int ans1=lcshelpermemo(s1+1,s2,m1-1,m2,dp); 19 | int ans2=lcshelpermemo(s1,s2+1,m1,m2-1,dp); 20 | ans= max(ans1,ans2); 21 | } 22 | dp[m1][m2]=ans; 23 | return ans; 24 | 25 | } 26 | int lcs(char *s1,char *s2) 27 | { 28 | int m=strlen(s1); 29 | int n=strlen(s2); 30 | int**dp=new int*[m+1]; 31 | for(int i=0;i>s1>>s2; 86 | 87 | cout< 2 | using namespace std; 3 | 4 | // int alphacode1helper(int size,int *arr,int *arrhelper) 5 | // { 6 | // if(size==0 || size==1) 7 | // { 8 | // return 1; 9 | // } 10 | // if(arrhelper[size]>0) 11 | // { 12 | // return arrhelper[size]; 13 | // } 14 | // int output=alphacode1helper(size-1,arr,arrhelper); 15 | // if((output[size-2]*10)+output[size-1]<=26) 16 | // { 17 | // output+=alphacode1helper(size-2,arr,arrhelper); 18 | // } 19 | // arrhelper[size]=output; 20 | // return output; 21 | // } 22 | // int alphacode1(int size,int *arr) 23 | // { 24 | // int *arrhelper=new int[size+1]; 25 | // for(int i=0;i>n; 53 | int *arr=new int[n]; 54 | cout<<"enter\n"; 55 | for(int i=0;i>arr[i]; 58 | } 59 | // cout<<"alphacode1: "< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int a[100][100]={0}; 8 | int m,n; 9 | cout<<"Enter rows and columns of 2D-Array:"; 10 | cin>>m>>n; 11 | 12 | int val=1; 13 | for(int row=0;row<=m-1;row++){ 14 | for(int col=0;col<=n-1;col++){ 15 | a[row][col]=val; 16 | val=val+1; 17 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void spiral_print(int a[100][100], int m, int n){ 6 | int startRow = 0; 7 | int startCol = 0; 8 | int endRow = m - 1; 9 | int endCol = n - 1; 10 | 11 | //Print it 12 | while (startRow <= endRow and startCol <= endCol) { 13 | //First Row 14 | 15 | for (int i = startCol; i <= endCol; i++) { 16 | cout << a[startRow][i] << " "; 17 | } 18 | startRow++; 19 | 20 | for (int i = startRow; i <= endRow; i++) { 21 | cout << a[i][endCol] << " "; 22 | } 23 | endCol--; 24 | 25 | //Bottom Row 26 | if (endRow > startRow) { 27 | for (int i = endCol; i >= startCol; i--) { 28 | cout << a[endRow][i] << " "; 29 | } 30 | endRow--; 31 | } 32 | 33 | 34 | //start col 35 | if (endCol > startCol) { 36 | for (int i = endRow; i >= startRow; i--) { 37 | cout << a[i][startCol] << " "; 38 | } 39 | startCol++; 40 | } 41 | } 42 | } 43 | 44 | int main(){ 45 | 46 | int a[100][100]={0}; 47 | int m,n; 48 | cout<<"Enter rows and columns of 2D-Array:"; 49 | cin>>m>>n; 50 | 51 | int val=1; 52 | for(int row=0;row<=m-1;row++){ 53 | for(int col=0;col<=n-1;col++){ 54 | a[row][col]=val; 55 | val=val+1; 56 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int a[100][100]={0}; 8 | int m,n; 9 | cout<<"Enter rows and columns of 2D-Array:"; 10 | cin>>m>>n; 11 | 12 | int val=1; 13 | for(int row=0;row<=m-1;row++){ 14 | for(int col=0;col<=n-1;col++){ 15 | a[row][col]=val; 16 | val=val+1; 17 | cout<=0;row--){ 33 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int arr[]={20,30,40,40,40,50,100,1100}; 7 | int n=sizeof(arr)/sizeof(int); 8 | int key; 9 | cout<<"Enter Key to find: "; 10 | cin>>key; 11 | 12 | bool present = binary_search(arr,arr+n,key); 13 | 14 | if(present){ 15 | cout<<"present"< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int arr[]={2,5,6,11,15,8}; 7 | int n=sizeof(arr)/sizeof(int); 8 | int key; 9 | cout<<"Enter Key to find: "; 10 | cin>>key; 11 | //Instead of auto it we can also use int* it 12 | auto it=find(arr,arr+n,key); 13 | int index=it-arr; 14 | if(index==n){ 15 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | 8 | int arr[]={10,20,30,40,50}; 9 | int n=sizeof(arr)/sizeof(int); 10 | rotate(arr,arr+2,arr+n); 11 | for(int i=0;i v{10,20,30,40,50}; 20 | rotate(v.begin(),v.begin()+3,v.end()); 21 | for(int i=0;i v1{1,2,3}; 27 | next_permutation(v1.begin(),v1.end()); 28 | next_permutation(v1.begin(),v1.end()); 29 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n, key; 8 | cout<<"Enter size of array"<>n; 10 | 11 | int a[1000]; 12 | 13 | cout<<"Enter elements of array"<>a[i]; 16 | } 17 | 18 | //Generate all subarrays 19 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int n, key; 7 | cout<<"Enter size of array"<>n; 9 | 10 | int a[1000]; 11 | 12 | cout<<"Enter elements of array"<>a[i]; 15 | } 16 | 17 | cout<<"Enter key to search"<>key; 19 | 20 | int i; 21 | 22 | for(i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n, key; 8 | cout<<"Enter size of array"<>n; 10 | 11 | int a[1000]; 12 | 13 | cout<<"Enter elements of array"<>a[i]; 16 | } 17 | 18 | int currentSum=0; 19 | int maxSum=0; 20 | int left=-1; 21 | int right=-1; 22 | 23 | for(int i=0;imaxSum 30 | if(currentSum>maxSum){ 31 | maxSum=currentSum; 32 | left=i; 33 | right=j; 34 | } 35 | } 36 | } 37 | 38 | cout<<"Maximum Sum: "< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n, key; 8 | cout<<"Enter size of array"<>n; 10 | 11 | int a[1000]; 12 | int cumSum[1000]={0}; 13 | 14 | cout<<"Enter elements of array"<>a[0]; 16 | cumSum[0]=a[0]; 17 | for(int i=1;i>a[i]; 19 | cumSum[i]=cumSum[i-1]+a[i]; 20 | } 21 | 22 | int currentSum=0; 23 | int maxSum=0; 24 | int left=-1; 25 | int right=-1; 26 | 27 | for(int i=0;imaxSum 31 | if(currentSum>maxSum){ 32 | maxSum=currentSum; 33 | left=i; 34 | right=j; 35 | } 36 | } 37 | } 38 | 39 | cout<<"Maximum Sum: "< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n, key; 8 | cout<<"Enter size of array"<>n; 10 | 11 | int a[1000]; 12 | int cumSum[1000]={0}; 13 | 14 | cout<<"Enter elements of array"<>a[0]; 16 | cumSum[0]=a[0]; 17 | for(int i=1;i>a[i]; 19 | cumSum[i]=cumSum[i-1]+a[i]; 20 | } 21 | 22 | int currentSum=0; 23 | int maxSum=0; 24 | 25 | 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int n,k,s=0; 6 | cin >> n>>k; 7 | int a[n]; 8 | for(int i=0;i> a[i]; 11 | } 12 | int start=0,end=n-1; 13 | for(int i=0;ik) 16 | { 17 | end--; 18 | } 19 | else if(a[start]+a[end] 4 | using namespace std; 5 | 6 | // Function to get the largest element from an array 7 | int getMax(int array[], int n) { 8 | int max = array[0]; 9 | for (int i = 1; i < n; i++) 10 | if (array[i] > max) 11 | max = array[i]; 12 | return max; 13 | } 14 | 15 | // Using counting sort to sort the elements in the basis of significant places 16 | void countingSort(int array[], int size, int place) { 17 | const int max = 10; 18 | int output[size]; 19 | int count[max]; 20 | 21 | for (int i = 0; i < max; ++i) 22 | count[i] = 0; 23 | 24 | // Calculate count of elements 25 | for (int i = 0; i < size; i++) 26 | count[(array[i] / place) % 10]++; 27 | 28 | // Calculate cumulative count 29 | for (int i = 1; i < max; i++) 30 | count[i] += count[i - 1]; 31 | 32 | // Place the elements in sorted order 33 | for (int i = size - 1; i >= 0; i--) { 34 | output[count[(array[i] / place) % 10] - 1] = array[i]; 35 | count[(array[i] / place) % 10]--; 36 | } 37 | 38 | for (int i = 0; i < size; i++) 39 | array[i] = output[i]; 40 | } 41 | 42 | // Main function to implement radix sort 43 | void radixsort(int array[], int size) { 44 | // Get maximum element 45 | int max = getMax(array, size); 46 | 47 | // Apply counting sort to sort elements based on place value. 48 | for (int place = 1; max / place > 0; place *= 10) 49 | countingSort(array, size, place); 50 | } 51 | 52 | // Print an array 53 | void printArray(int array[], int size) { 54 | int i; 55 | for (i = 0; i < size; i++) 56 | cout << array[i] << " "; 57 | cout << endl; 58 | } 59 | 60 | // Driver code 61 | int main() { 62 | int array[] = {121, 432, 564, 23, 1, 45, 788}; 63 | int n = sizeof(array) / sizeof(array[0]); 64 | radixsort(array, n); 65 | printArray(array, n); 66 | } 67 | -------------------------------------------------------------------------------- /DSA/Arrays/Remove duplicate elements from sorted array.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | 6. Remove duplicate elements from sorted Array 3 | Given a sorted array A of size N, delete all the duplicates elements from A. 4 | Note: Don't use set or HashMap to solve the problem. 5 | 6 | 7 | Example 1: 8 | 9 | Input: 10 | N = 5 11 | Array = {2, 2, 2, 2, 2} 12 | Output: 2 13 | Explanation: After removing all the duplicates 14 | only one instance of 2 will remain. 15 | Example 2: 16 | 17 | Input: 18 | N = 3 19 | Array = {1, 2, 2} 20 | Output: 1 2 21 | 22 | Your Task: 23 | You dont need to read input or print anything. Complete the function remove_duplicate() which takes the array A[] and its size N as input parameters and modifies it in place to delete all the duplicates. The function must return an integer X denoting the new modified size of the array. 24 | Note: The generated output will print all the elements of the modified array from index 0 to X-1. 25 | 26 | 27 | Expected Time Complexity: O(N) 28 | Expected Auxiliary Space: O(1) 29 | 30 | 31 | Constraints: 32 | 1 ≤ N ≤ 104 33 | 1 ≤ A[i] ≤ 106 34 | **/ 35 | // { Driver Code Starts 36 | //Initial template for C++ 37 | 38 | #include 39 | using namespace std; 40 | 41 | 42 | // } Driver Code Ends 43 | //User function template for C++ 44 | 45 | class Solution{ 46 | public: 47 | int remove_duplicate(int a[],int n){ 48 | // code here 49 | } 50 | }; 51 | 52 | // { Driver Code Starts. 53 | int main() 54 | { 55 | int T; 56 | cin>>T; 57 | while(T--) 58 | { 59 | int N; 60 | cin>>N; 61 | int a[N]; 62 | for(int i=0;i>a[i]; 65 | } 66 | Solution ob; 67 | int n = ob.remove_duplicate(a,N); 68 | 69 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >>n; 7 | int a[n]; 8 | for(int i=0;i> a[i]; 11 | } 12 | const int N=1e+2; 13 | int indx[N]; 14 | 15 | for(int i=0;i=0) 22 | { 23 | indx[a[i]]++; 24 | } 25 | } 26 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int n, key; 8 | cout<<"Enter size of array"<>n; 10 | 11 | int a[1000]; 12 | 13 | cout<<"Enter elements of array"<>a[i]; 16 | } 17 | 18 | int largest=INT_MIN; 19 | int smallest=INT_MAX; 20 | 21 | /* 22 | for(int i=0;ilargest){ 24 | largest=a[i]; 25 | } 26 | if(a[i] 2 | using namespace std; 3 | 4 | int binary_search(int a[], int n, int key){ 5 | int start=0; 6 | int end=n-1; 7 | while(start<=end){ 8 | int mid=(start+end)/2; 9 | if(a[mid]==key){ 10 | return mid; 11 | } 12 | else if(a[mid]>key){ 13 | end=mid-1; 14 | } 15 | else{ 16 | start=mid+1; 17 | } 18 | } 19 | return -1; 20 | } 21 | 22 | int main(){ 23 | 24 | int n, key; 25 | cout<<"Enter size of array"<>n; 27 | 28 | int a[1000]; 29 | 30 | cout<<"Enter elements of array"<>a[i]; 33 | } 34 | 35 | cout<<"Enter key to search"<>key; 37 | 38 | cout< 2 | using namespace std; 3 | 4 | void bubble_sort(int a[], int n){ 5 | for(int i=1;i<=n-1;i++){ 6 | for(int j=0;j<=(n-i-1);j++){ 7 | if(a[j]>a[j+1]){ 8 | swap(a[j],a[j+1]); 9 | } 10 | } 11 | } 12 | } 13 | 14 | int main(){ 15 | 16 | int n, key; 17 | cout<<"Enter size of array"<>n; 19 | 20 | int a[1000]; 21 | 22 | cout<<"Enter elements of array"<>a[i]; 25 | } 26 | 27 | bubble_sort(a,n); 28 | 29 | for(int i=0;i 2 | using namespace std; 3 | 4 | bool compare(int a, int b){ 5 | return a>n; 23 | 24 | int a[1000]; 25 | 26 | cout<<"Enter elements of array"<>a[i]; 29 | } 30 | 31 | bubble_sort(a,n,compare); 32 | 33 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | // define a comparator function 6 | bool compare(int a, int b){ 7 | return a>b; 8 | } 9 | 10 | int main(){ 11 | 12 | int n, key; 13 | cout<<"Enter size of array"<>n; 15 | 16 | int a[1000]; 17 | 18 | cout<<"Enter elements of array"<>a[i]; 21 | } 22 | 23 | //sort an array using sort() function, more efficient 24 | sort(a,a+n,compare); 25 | 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | void insertion_sort(int a[], int n){ 5 | for(int i=1;i<=n-1;i++){ 6 | int e=a[i]; 7 | //place the current element at 'right' position in the sorted part 8 | int j=i-1; 9 | while(j>=0 and a[j]>e){ 10 | a[j+1]=a[j]; 11 | j=j-1; 12 | } 13 | a[j+1]=e; 14 | } 15 | } 16 | 17 | int main(){ 18 | 19 | int n, key; 20 | cout<<"Enter size of array"<>n; 22 | 23 | int a[1000]; 24 | 25 | cout<<"Enter elements of array"<>a[i]; 28 | } 29 | 30 | insertion_sort(a,n); 31 | 32 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* For a given array arr[], 5 | returns the maximum j – i such that 6 | arr[j] > arr[i] */ 7 | int maxIndexDiff(int arr[], int n) 8 | { 9 | 10 | int rightMax[n]; 11 | rightMax[n-1]= arr[n-1]; 12 | for(int i = n-2; i>=0; i--) 13 | rightMax[i] = max(rightMax[i+1] , arr[i]); 14 | 15 | //rightMax[i] = max{ arr[i...(n-1] } 16 | 17 | 18 | 19 | int maxDist = INT_MIN; 20 | int i = 0, j = 0; 21 | while(i= arr[i]) 24 | { 25 | maxDist = max( maxDist, j-i ); 26 | j++; 27 | } 28 | else // if(rightMax[j] < leftMin[i]) 29 | i++; 30 | } 31 | 32 | return maxDist; 33 | 34 | 35 | } 36 | 37 | // Driver Code 38 | int main() 39 | { 40 | int arr[] = { 34,8,10,3,2,80,30,33,1}; 41 | int n = sizeof(arr) / sizeof(arr[0]); 42 | int maxDiff = maxIndexDiff(arr, n); 43 | cout << maxDiff; 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /DSA/Arrays/pair_sum(two_pointer_approach).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int a[]={1,3,5,7,10,11,12,13}; 8 | int s=16; 9 | 10 | int i=0; 11 | int j=sizeof(a)/sizeof(int)-1; 12 | 13 | while(is){ 16 | j--; 17 | } 18 | else if(currentSum 2 | using namespace std; 3 | 4 | void selection_search(int a[], int n){ 5 | for(int i=0;i>n; 22 | 23 | int a[1000]; 24 | 25 | cout<<"Enter elements of array"<>a[i]; 28 | } 29 | 30 | selection_search(a,n); 31 | 32 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | int **arr; 8 | int r,c; 9 | cout<<"Enter total no. of rows and columns: "; 10 | cin>>r>>c; 11 | 12 | //Create an array of row heads 13 | arr=new int*[r]; 14 | 15 | //Create an 2D array 16 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | cout<<"Static Allocation / Compile Time Allocation"<>n; 22 | int *a = new int[n]; 23 | cout<<"Size of Array: "; 24 | cout<>a[i]; 34 | cout< 2 | #include 3 | using namespace std; 4 | 5 | //char *strtok(char*s,char *delimiters) 6 | //we use it to break a string into a list of tokens 7 | //returns a token on each subsequent call 8 | //on the first call function should be passed with string argument for 's' 9 | //on subsequent calls we should pass the string argument as null 10 | 11 | int main(){ 12 | 13 | char s[100]= "Today is a rainy day"; 14 | 15 | 16 | // Delimeter with " " (space) 17 | char *ptr = strtok(s," "); 18 | cout< 2 | using namespace std; 3 | 4 | void addedge(vector> &adj,int u,int v) 5 | { 6 | adj[u].push_back(v); 7 | } 8 | 9 | void printgraph(vector>&adj) 10 | { 11 | int i=0; 12 | for(auto x: adj) 13 | { 14 | cout< "; 15 | for( auto l:x) 16 | { 17 | cout<< l<<" | "; 18 | } 19 | cout<>&adj,int u,vector&visited,vector&instack) 24 | { 25 | 26 | //process the vertex 27 | visited[u]=true; 28 | instack[u]=true; 29 | for(auto x:adj[u]) 30 | { 31 | if(visited[x]==false && dfs_rec(adj,x,visited,instack)) 32 | return true; 33 | else if(instack[x])return true; 34 | } 35 | instack[u]=false; 36 | return false; 37 | } 38 | 39 | 40 | bool dfs(vector>&adj,int size) 41 | { 42 | vectorvisited(size,false); 43 | vectorinstack(size,false); 44 | for(int i=0;i>adj(size); 57 | addedge(adj,0,1); 58 | addedge(adj,2,1); 59 | addedge(adj,2,3); 60 | addedge(adj,3,4); 61 | addedge(adj,4,5); 62 | addedge(adj,5,3); 63 | printgraph(adj); 64 | if(dfs(adj,size))cout<<"cycle found"< 8 | using namespace std; 9 | 10 | int main(){ 11 | vector nums{5,7,7,8,8,10}; 12 | vector v; 13 | int target=8; 14 | 15 | bool present = binary_search(nums.begin(),nums.end(),target); 16 | int index_lb,index_ub; 17 | if(present){ 18 | auto lb=lower_bound(nums.begin(),nums.end(),target); 19 | index_lb=lb-nums.begin(); 20 | v.push_back(index_lb); 21 | 22 | auto ub=upper_bound(nums.begin(),nums.end(),target); 23 | index_ub=ub-nums.begin()-1; 24 | v.push_back(index_ub); 25 | } 26 | else{ 27 | index_lb=-1; 28 | index_ub=-1; 29 | v.push_back(index_lb); 30 | v.push_back(index_ub); 31 | } 32 | 33 | for(int i=0;i 2 | using namespace std; 3 | 4 | void heapify(int arr[], int n, int i) 5 | { 6 | int largest = i; 7 | int l = 2*i + 1; 8 | int r = 2*i + 2; 9 | if (l < n && arr[l] > arr[largest]) 10 | largest = l; 11 | 12 | if (r < n && arr[r] > arr[largest]) 13 | largest = r; 14 | 15 | if (largest != i) 16 | { 17 | swap(arr[i], arr[largest]); 18 | heapify(arr, n, largest); 19 | } 20 | } 21 | 22 | void buildheap(int arr[],int n){ 23 | for (int i = n / 2 - 1; i >= 0; i--) 24 | heapify(arr, n, i); 25 | } 26 | 27 | void heapSort(int arr[], int n) 28 | { 29 | buildheap(arr,n); 30 | 31 | for (int i=n-1; i>0; i--) 32 | { 33 | swap(arr[0], arr[i]); 34 | heapify(arr, i, 0); 35 | } 36 | } 37 | 38 | int main(void) 39 | { 40 | int a[10]; 41 | cout << "Enter 10 elements of array: " << endl; 42 | for (int i=0; i<10; i++) 43 | cin >> a[i]; 44 | cout << "Original Array is: "; 45 | for (int i=0; i<10; i++) 46 | cout << a[i] << " "; 47 | int n=sizeof(a)/sizeof(a[0]); 48 | cout <<"\nArray after sorting is: "; 49 | heapSort(a,n); 50 | for(int x: a) 51 | cout< 2 | using namespace std; 3 | 4 | struct min_heap_node { 5 | char data; 6 | unsigned freq; 7 | min_heap_node *left, *right; 8 | min_heap_node(char data, unsigned freq) 9 | { 10 | left = right = NULL; 11 | this->data = data; 12 | this->freq = freq; 13 | } 14 | }; 15 | 16 | struct compare { 17 | bool operator()(min_heap_node* l, min_heap_node* r) 18 | { 19 | return (l->freq > r->freq); 20 | } 21 | }; 22 | 23 | void print_codes(struct min_heap_node* root, string str) 24 | { 25 | 26 | if (!root) 27 | return; 28 | if (root->data != '$') 29 | cout << root->data << ": " << str << "\n"; 30 | print_codes(root->left, str + "0"); 31 | print_codes(root->right, str + "1"); 32 | } 33 | 34 | void huff_code(char data[], int freq[], int size) 35 | { 36 | struct min_heap_node *left, *right, *top; 37 | priority_queue, compare> min_heap; 38 | 39 | for (int i = 0; i < size; ++i) 40 | min_heap.push(new min_heap_node(data[i], freq[i])); 41 | while (min_heap.size() != 1) { 42 | left = min_heap.top(); 43 | min_heap.pop(); 44 | 45 | right = min_heap.top(); 46 | min_heap.pop(); 47 | top = new min_heap_node('$', left->freq + right->freq); 48 | top->left = left; 49 | top->right = right; 50 | 51 | min_heap.push(top); 52 | } 53 | print_codes(min_heap.top(), ""); 54 | } 55 | 56 | 57 | int main() 58 | { 59 | 60 | char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; 61 | int freq[] = { 5, 9, 12, 13, 16, 45 }; 62 | 63 | int size = sizeof(arr) / sizeof(arr[0]); 64 | 65 | huff_code(arr, freq, size); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void InsertionSort(int list[], int n) 6 | { 7 | int i, flag, j; 8 | for (i = 1; i < n; i++) 9 | { 10 | flag = list[i]; 11 | j = i - 1; 12 | 13 | 14 | while (j >= 0 && list[j] >flag) 15 | { 16 | list[j + 1] = list[j]; 17 | j = j - 1; 18 | } 19 | list[j + 1] = flag; 20 | } 21 | } 22 | 23 | 24 | void PrintArray(int list[], int n) 25 | { 26 | int i; 27 | for (i = 0; i < n; i++) 28 | cout << list[i] << " "; 29 | cout << endl; 30 | } 31 | int main() 32 | { 33 | int list[] = { 2, 1, 3, 15, 26 }; 34 | int n = sizeof(list) / sizeof(list[0]); 35 | 36 | InsertionSort(list, n); 37 | PrintArray(list, n); 38 | 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /Knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 105 3 | #define WMAX 205 4 | 5 | struct item{ 6 | int profit; 7 | int weight; 8 | }; 9 | 10 | using namespace std; 11 | 12 | item arr[N]; 13 | int M[N][WMAX]; 14 | 15 | void find_solution(int i, int w){ 16 | if (i == 0){ 17 | return; 18 | } 19 | if (M[i][w] == M[i-1][w]){ 20 | find_solution(i-1, w); 21 | } 22 | else{ 23 | find_solution(i-1, w - arr[i].weight); 24 | printf("%d " ,i ); 25 | } 26 | } 27 | 28 | int main(){ 29 | int n, p, W; 30 | scanf("%d %d" ,&n ,&W); 31 | 32 | for (int i = 1; i <= n; i++){ 33 | scanf("%d %d" ,&arr[i].profit ,&arr[i].weight); 34 | } 35 | 36 | for (int i = 0; i <= W; i++){ 37 | M[0][i] = 0; 38 | } 39 | 40 | for (int i = 1; i <= n; i++){ 41 | for (int w_ = 1; w_ <= W; w_++){ 42 | if (arr[i].weight > w_){ 43 | M[i][w_] = M[i-1][w_]; 44 | } 45 | else{ 46 | M[i][w_] = max(M[i-1][w_], arr[i].profit + M[i-1][w_ - arr[i].weight]); 47 | } 48 | } 49 | } 50 | 51 | printf("profit: %d\n" ,M[n][W]); 52 | printf("solution set: "); 53 | find_solution(n, W); 54 | printf("\n"); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /LUCKYNUM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #define pb(x) push_back(x) 5 | #define mp(a,b) make_pair(a,b) 6 | const ll mod =1e9+7; 7 | void solve() 8 | { 9 | ll t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | ll a,b,c; 14 | cin>>a>>b>>c; 15 | if(a==7||b==7||c==7) 16 | { 17 | cout<<"YES"<next); 9 | } 10 | TreeNode* sortedListToBST(ListNode* head) { 11 | if(head==NULL) 12 | return NULL; 13 | else if(head->next==NULL) 14 | { 15 | TreeNode* root=new TreeNode(head->val); 16 | return root; 17 | } 18 | else 19 | { 20 | int size1=lengthoflinkedlist(head)/2; 21 | ListNode* prev=head; 22 | ListNode* temp=head; 23 | while(size1--) 24 | { 25 | prev=temp; 26 | temp=temp->next; 27 | } 28 | prev->next=NULL; 29 | TreeNode* root=new TreeNode(temp->val); 30 | root->left=sortedListToBST(head); 31 | root->right=sortedListToBST(temp->next); 32 | return root; 33 | 34 | } 35 | // return root; 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/LinkedList/LinkedListCycle1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasCycle(ListNode *head) { 4 | 5 | if(head== NULL) 6 | { 7 | return false; 8 | } 9 | ListNode *slowptr=head; 10 | ListNode *fastptr=head; 11 | 12 | while(fastptr!=NULL && fastptr->next!=NULL) 13 | { 14 | slowptr=slowptr->next; 15 | fastptr=fastptr->next->next; 16 | if(slowptr==fastptr) 17 | { 18 | return true; 19 | } 20 | 21 | } 22 | return false; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/LinkedList/LinkedListCycle2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *detectCycle(ListNode *head) { 4 | bool check=false; 5 | ListNode *slowptr=head; 6 | ListNode *fastptr=head; 7 | while(fastptr!=NULL && fastptr->next!=NULL) 8 | { 9 | slowptr=slowptr->next; 10 | fastptr=fastptr->next->next; 11 | if(slowptr ==fastptr ) 12 | { 13 | check=true; 14 | break; 15 | } 16 | } 17 | if(check==false) 18 | return NULL; 19 | slowptr=head; 20 | // fastptr=head; 21 | while(slowptr!=fastptr) 22 | { 23 | slowptr=slowptr->next; 24 | fastptr=fastptr->next; 25 | } 26 | return slowptr; 27 | } 28 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/LinkedList/RemoveDuplicatesinLL.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* deleteDuplicates(ListNode* head) { 4 | ListNode*head1=head; 5 | while(head!=NULL && head->next!=NULL) 6 | { 7 | if(head->val==head->next->val) 8 | { 9 | head->next=head->next->next; 10 | 11 | } 12 | else 13 | { 14 | head=head->next; 15 | } 16 | 17 | } 18 | return head1; 19 | } 20 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/LinkedList/RemoveNthNodeFromEndofLL.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sizeget(ListNode*head) 4 | { 5 | int ctr=0; 6 | ListNode*head1=head; 7 | while(head1!=NULL) 8 | { 9 | ctr++; 10 | head1=head1->next; 11 | } 12 | return ctr; 13 | } 14 | ListNode* removeNthFromEnd(ListNode* head, int n) { 15 | 16 | int size=sizeget(head); 17 | ListNode*head1=head; 18 | ListNode*temp; 19 | int ctr=0; 20 | if(size==n) 21 | return head->next; 22 | while(head1!=NULL) 23 | { 24 | if(ctr==size-n-1) 25 | { 26 | break; 27 | } 28 | ctr++; 29 | head1=head1->next; 30 | } 31 | 32 | temp=head1->next; 33 | head1->next=temp->next; 34 | 35 | return head; 36 | } 37 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/LinkedList/mergetwoSortedLL.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { 4 | 5 | ListNode*res; 6 | if(list1==NULL) 7 | return list2; 8 | if(list2==NULL) 9 | return list1; 10 | if(list1->val>=list2->val) 11 | { 12 | res=list2; 13 | res->next=mergeTwoLists(list1,list2->next); 14 | } 15 | else 16 | { 17 | res=list1; 18 | res->next=mergeTwoLists(list1->next,list2); 19 | 20 | } 21 | return res; 22 | } 23 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/LinkedList/swapNodeinPairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* swapPairs(ListNode* head) { 4 | ListNode* newnode=new ListNode(-1); 5 | newnode->next=head; 6 | ListNode*prev=newnode; 7 | ListNode*current=head; 8 | 9 | while(current!=NULL && current->next!=NULL) 10 | { 11 | prev->next=current->next; 12 | current->next=prev->next->next; 13 | prev->next->next=current; 14 | current=current->next; 15 | prev=prev->next->next; 16 | } 17 | return newnode->next; 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/array/3sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | 5 | vector>v1; 6 | sort(nums.begin(),nums.end()); 7 | int n=nums.size(); 8 | 9 | 10 | for(int i=0;i0 && nums[i]==nums[i-1]) 13 | continue; 14 | int start=i+1; 15 | int end=n-1; 16 | int find = -1*nums[i]; 17 | while(startfind) 25 | { 26 | end--; 27 | } 28 | else 29 | { 30 | vectorv2={nums[i],nums[start],nums[end]}; 31 | v1.push_back(v2); 32 | while(start& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int flag; 6 | for(int i=0;i& nums) { 4 | int n=nums.size(); 5 | int i=0; 6 | int j=1; 7 | while(j& prices) { 4 | int maxP=0; 5 | int minP=INT_MAX; 6 | for(int i=0;i findDuplicates(vector& nums) { 4 | vector v; 5 | for(int ele:nums) 6 | { 7 | ele=abs(ele); 8 | if(nums[ele-1]>0) 9 | nums[ele-1]*=-1; 10 | else 11 | v.push_back(ele); 12 | } 13 | return v; 14 | } 15 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/array/majorityelement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | 5 | sort(nums.begin(),nums.end()); 6 | return nums[nums.size()/2]; 7 | } 8 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/array/mergesort.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void merge(vector& nums1, int m, vector& nums2, int n) { 4 | 5 | int temparr[m+n]; 6 | int i=0,j=0,k=0; 7 | 8 | while(inums2[j]) 17 | { 18 | temparr[k]=nums2[j]; 19 | 20 | j++; 21 | } 22 | else 23 | { 24 | temparr[k]=nums1[i]; 25 | i++; 26 | k++; 27 | temparr[k]=nums2[j++]; 28 | } 29 | k++; 30 | 31 | } 32 | while(i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int findMinDiff(int arr[],int n , int m) 8 | { 9 | sort(arr[0],arr[n-1]); 10 | int j=m-1; 11 | int d=INT_MAX; 12 | for(int i=0;i+j& nums) { 4 | 5 | int j=0; 6 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector removeDuplicate(string arr) 7 | { 8 | vectoroutput; 9 | unordered_mapchecknum; 10 | for(int i=0;i0) 13 | { 14 | continue; 15 | } 16 | checknum[arr[i]]=true; 17 | output.push_back(arr[i]); 18 | } 19 | 20 | return output; 21 | } 22 | 23 | int main() 24 | { 25 | string arr="abcdsfdfgdfasaaa"; 26 | // int length=arr.length(); 27 | vectoroutput= removeDuplicate(arr); 28 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | void sortColors(vector& nums) { 8 | 9 | sort(nums.begin(),nums.end()); 10 | 11 | for(int i=0;i spiralOrder(vector>& matrix) { 4 | vector ans; 5 | int top=0,bottom=matrix.size()-1,left=0,right=matrix[0].size()-1,dir=0; 6 | while(top<=bottom && left<=right){ 7 | if(dir==0){ //left to right 8 | for(int i=left;i<=right;i++){ 9 | ans.push_back(matrix[top][i]); 10 | } 11 | top++; 12 | } 13 | else if(dir==1){ //top to bottom 14 | for(int i=top;i<=bottom;i++){ 15 | ans.push_back(matrix[i][right]); 16 | } 17 | right--; 18 | } 19 | else if(dir==2){ //right to left 20 | for(int i=right;i>=left;i--){ 21 | ans.push_back(matrix[bottom][i]); 22 | } 23 | bottom--; 24 | } 25 | else if(dir==3){ //bottom to top 26 | for(int i=bottom;i>=top;i--){ 27 | ans.push_back(matrix[i][left]); 28 | } 29 | left++; 30 | } 31 | dir=(dir+1)%4; //changing direction 32 | } 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/array/strStr.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string haystack, string needle) { 4 | if(needle.length()==0) 5 | return 0; 6 | if(haystack.length()>=needle.length()) 7 | { 8 | for(int i=0;i twoSum(vector& nums, int target) { 4 | 5 | vectorv; 6 | int s=nums.size(); 7 | int st=0; 8 | int en=s-1; 9 | sort(nums.begin(),nums.end()); 10 | while(sttarget) 21 | { 22 | en-=1; 23 | } 24 | else 25 | { 26 | st+=1; 27 | } 28 | } 29 | } 30 | return v; 31 | } 32 | }; -------------------------------------------------------------------------------- /Leetcode Important questions/array/valid_parenthesis.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string s) { 4 | stack temp; 5 | for(int i=0;i& height) 4 | { 5 | int start=0;int end=height.size()-1; 6 | int maxi=INT_MIN; 7 | while(start 2 | using namespace std; 3 | void linearSearch(int *a,int i,int n,int key) 4 | { 5 | // If element is not present then it will return -1 6 | if(i==n) 7 | { 8 | cout<<"element not found"; 9 | } 10 | //If the element is present then it will return the index where the element is present 11 | if(a[i]==key) 12 | { 13 | cout<<"element is found and the element is "< 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 | void Display(struct Node *p) 28 | { 29 | while(p!=NULL) 30 | { 31 | printf("%d ",p->data); 32 | p=p->next; 33 | } 34 | } 35 | void RDisplay(struct Node *p) 36 | { 37 | if(p!=NULL) 38 | { 39 | RDisplay(p->next); 40 | printf("%d ",p->data); 41 | 42 | } 43 | } 44 | int main() 45 | { 46 | struct Node *temp; 47 | int A[]={3,5,7,10,25,8,32,2}; 48 | create(A,8); 49 | 50 | Display(first); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Linked List/02.Create and Display a linked list.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; idata = 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 | } -------------------------------------------------------------------------------- /Linked List/03.Recursive display of linked list.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 *t,*last; 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;idata=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 | RDisplay(p->next); 39 | printf("%d ",p->data); 40 | 41 | } 42 | } 43 | int main() 44 | { 45 | struct Node *temp; 46 | int A[]={3,5,7,10,25,8,32,2}; 47 | create(A,8); 48 | 49 | Display(first); 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Linked List/04.Count and sum.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 *t,*last; 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;idata=A[i]; 21 | t->next=NULL; 22 | last->next=t; 23 | last=t; 24 | } 25 | } 26 | int count(struct Node *p) 27 | { 28 | int l=0; 29 | while(p) 30 | { 31 | l++; 32 | p=p->next; 33 | } 34 | return l; 35 | } 36 | int Rcount(struct Node *p) 37 | { 38 | if(p!=NULL) 39 | return Rcount(p->next)+1; 40 | else 41 | return 0; 42 | } 43 | int sum(struct Node *p) 44 | { 45 | int s=0; 46 | 47 | while(p!=NULL) 48 | { 49 | s+=p->data; 50 | p=p->next; 51 | } 52 | return s; 53 | } 54 | int Rsum(struct Node *p) 55 | { 56 | if(p==NULL) 57 | return 0; 58 | else 59 | return Rsum(p->next)+p->data; 60 | } 61 | int main() 62 | { 63 | int A[]={3,5,7,10,25,8,32,2}; 64 | create(A,8); 65 | 66 | printf("Count %d\n",count(first)); 67 | printf("Sum %d\n",sum(first)); 68 | 69 | 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Linked List/05.Printing the maximum 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 | printf("Max %d\n",Max(first)); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Linked List/07.Searching an element.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 * 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 | } -------------------------------------------------------------------------------- /Linked List/08.Searching an element 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* t, * last; 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) // displaying the elements 27 | { 28 | while (p != NULL) 29 | { 30 | printf(" %d ", p->data); 31 | p = p->next; 32 | } 33 | } 34 | struct Node* LSearch(struct Node* p, int key) 35 | { 36 | struct Node* q=NULL; 37 | 38 | while (p != NULL) 39 | { 40 | if (key == p->data) 41 | { 42 | q->next = p->next; // this block of code for move to first 43 | p->next = first; 44 | first = p; 45 | return p; 46 | } 47 | q = p; 48 | p = p->next; 49 | } 50 | return NULL; 51 | 52 | } 53 | struct Node* RSearch(struct Node* p, int key) 54 | { 55 | if (p == NULL) 56 | return NULL; 57 | if (key == p->data) 58 | return p; 59 | return RSearch(p->next, key); 60 | 61 | } 62 | int main() 63 | { 64 | struct Node* temp; 65 | int A[] = { 3,5,7,10,25,8,32,2 }; 66 | create(A, 8); 67 | temp = LSearch(first, 8); // this will occupy the 2nd index 68 | temp = LSearch(first, 2); // first this will go to 1st index 69 | //printf("% d", temp->data); 70 | Display(first); 71 | 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Linked List/09.Insertion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Node 4 | { 5 | int data; 6 | struct Node* next; 7 | 8 | }*first=NULL; 9 | 10 | void Create(int a[], int n) 11 | { 12 | int i; 13 | struct Node* t, * last; 14 | first = (struct Node*)malloc(sizeof(struct Node)); 15 | first->data = a[0]; 16 | first->next = NULL; 17 | last = first; 18 | 19 | for (i = 1; i < n; i++) 20 | { 21 | t = (struct Node*)malloc(sizeof(struct Node)); 22 | t->data = a[i]; 23 | t->next = NULL; 24 | last->next = t; 25 | last = t; 26 | } 27 | } 28 | 29 | void Display(struct Node* p) 30 | { 31 | while (p != NULL) 32 | { 33 | printf("%d ", p->data); 34 | p = p->next; 35 | } 36 | } 37 | void insert(struct Node *p , int pos, int x) // creating positioin and the value to be inserted 38 | { 39 | struct Node* t; // intializing node first 40 | if (pos == 0) 41 | { 42 | t = (struct Node*)malloc(sizeof(struct Node)); // Creating new node to enter value 43 | t->data = x; // Inserting Data to the node 44 | t->next = first; // points to first 45 | first = t; // Bringing 1st on the new node 46 | } 47 | else if (pos > 0) 48 | { 49 | p = first; 50 | for (int i = 0; i < pos - 1 && p; i++) // i shoud be position -1 and weather p is pointing on any node or not 51 | p = p->next; 52 | if (p) 53 | { 54 | t = (struct Node*)malloc(sizeof(struct Node)); 55 | t->data = x; 56 | t->next = p->next; 57 | p->next = t; 58 | } 59 | } 60 | } 61 | 62 | int main() 63 | { 64 | int a[] = { 1,2,4,5,7,4,5,44 }; 65 | Create(a, 8); 66 | insert(first, 0, 5); 67 | Display(first); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Linked List/10.Circular linked list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Node 4 | { 5 | int data; 6 | struct Node* next; // Self refrencial pointer 7 | }*Head; // global pointer we will use it in entire program 8 | 9 | void Create(int A[], int n) 10 | { 11 | int i; // creating a varible for iteration 12 | struct Node* t, * last; // t is for creating 1st node and last will always help in adding new noe at last 13 | Head = (struct Node*)malloc(sizeof(struct Node)); // allocating memory in heap // create 1st node and make head point on that node 14 | Head->data = A[0]; // passing data to 1st node 15 | Head->next = Head;// Head will point on head itself so that it can form a circular linked list 16 | last = Head; 17 | 18 | for (i = 1; i < n; i++) 19 | { 20 | t = (struct Node*)malloc(sizeof(struct Node)); // everytime we will create a new node with this 21 | t->data = A[i]; // everytime assigning data from the array to linkedlist 22 | t->next = last->next; // since it is circular so i acnnot use null here , this is linking new node next with last next (head) 23 | last->next = t; 24 | last = t; 25 | 26 | } 27 | 28 | 29 | } 30 | void Display(struct Node* h) // This is for Displaying Normal 31 | { 32 | do 33 | { 34 | printf("->%d ", h->data); 35 | h = h->next; 36 | } while (h != Head); 37 | printf("\n"); 38 | } 39 | 40 | void RDisplay(struct Node* h) 41 | { 42 | static int flag = 0; 43 | if (h != Head || flag == 0) 44 | { 45 | flag = 1; 46 | printf("->%d ", h->data); 47 | RDisplay(h->next); 48 | } 49 | flag = 0; 50 | 51 | } 52 | 53 | 54 | int main() 55 | { 56 | int A[] = { 2,3,4,5,6,7,89,98 }; 57 | Create(A, sizeof (A)/sizeof (A[0])); 58 | Display(Head); 59 | RDisplay(Head); 60 | 61 | return 0; 62 | } -------------------------------------------------------------------------------- /Linked List/11.Insert in sorted linked list.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 | } -------------------------------------------------------------------------------- /Linked List/13.Removing the duplicate.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 | // Remove duplicate from ll ; if nodes data are same del that ; from sorted linkedlist 44 | void Removeduplicate(struct Node* p) // taking pointer to 1st node 45 | { 46 | // inside fxn we want one more pointer 47 | struct Node* q = p->next; // which will before p // q is ahead and p is following 48 | while (q != NULL) 49 | { 50 | if (p->data != q->data) // if data is not matching // means no duplicate 51 | { 52 | p = q; // move ahead 53 | q = q->next; 54 | } 55 | else // if there is duplicate value 56 | { 57 | p->next = q->next; // p next points on q next //q will be logically remove from ll 58 | free(q); // delete q 59 | q = p->next; // and move q to next 60 | } 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | int main() 68 | { 69 | int A[] = {10,10,10,10,20,20,20,30,40,50 }; 70 | Create(A, sizeof (A)/sizeof (int )); 71 | Removeduplicate(first); 72 | Display(first); 73 | printf("\n\n"); 74 | 75 | return 0; 76 | 77 | } -------------------------------------------------------------------------------- /Linked List/LinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*next; 8 | 9 | //constructor 10 | node(int d){ 11 | data = d; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | //Linked Class (object oriented) 17 | /* 18 | class LinkedList{ 19 | node*head; 20 | node*tail; 21 | 22 | public: 23 | LinkedList(){ 24 | 25 | } 26 | void.insert(int d){ 27 | 28 | } 29 | ... 30 | ... 31 | }; 32 | */ 33 | 34 | //Function (Procedural Programming) 35 | 36 | void build(){ 37 | } 38 | 39 | //passing a pointer by reference 40 | void insertAtHead(node*&head, int d){ 41 | if(head==NULL){ 42 | head = new node(d); 43 | return; 44 | } 45 | 46 | node *n = new node(d); 47 | n->next = head; 48 | head = n; 49 | } 50 | 51 | void print(node*head){ 52 | while(head!=NULL){ 53 | cout<data<<"-->"; 54 | head = head->next; 55 | } 56 | cout< 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int data; 7 | Node(int d){ 8 | data = d; 9 | } 10 | Node *ptr; 11 | }; 12 | 13 | // Function to check if the linked list 14 | // is palindrome or not 15 | bool isPalin(Node* head){ 16 | 17 | // Temp pointer 18 | Node* slow= head; 19 | 20 | // Declare a stack 21 | stack s; 22 | 23 | 24 | // Push all elements of the list 25 | // to the stack 26 | while(slow != NULL){ 27 | s.push(slow->data); 28 | 29 | // Move ahead 30 | slow = slow->ptr; 31 | } 32 | 33 | // Iterate in the list again and 34 | // check by popping from the stack 35 | while(head != NULL ){ 36 | 37 | // Get the top most element 38 | int i=s.top(); 39 | 40 | // Pop the element 41 | s.pop(); 42 | 43 | // Check if data is not 44 | // same as popped element 45 | if(head -> data != i){ 46 | return false; 47 | } 48 | 49 | // Move ahead 50 | head=head->ptr; 51 | } 52 | 53 | return true; 54 | } 55 | 56 | int main(){ 57 | 58 | // Addition of linked list 59 | Node one = Node(1); 60 | Node two = Node(2); 61 | Node three = Node(3); 62 | Node four = Node(2); 63 | Node five = Node(1); 64 | 65 | // Initialize the next pointer 66 | // of every current pointer 67 | five.ptr = NULL; 68 | one.ptr = &two; 69 | two.ptr = &three; 70 | three.ptr = &four; 71 | four.ptr = &five; 72 | Node* temp = &one; 73 | 74 | 75 | int result = isPalin(&one); 76 | 77 | if(result == 1) 78 | cout<<"isPalindrome is true\n"; 79 | else 80 | cout<<"isPalindrome is true\n"; 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Linked List/circular_linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*next; 8 | 9 | //constructor 10 | node(int d){ 11 | data = d; 12 | next = NULL; 13 | } 14 | }; 15 | 16 | 17 | node* getNode(node*head,int data){ 18 | node*temp=head; 19 | while(temp->next!=head){ 20 | if(temp->data==data){ 21 | return temp; 22 | } 23 | temp=temp->next; 24 | } 25 | //when we are out of the loop, that means data is not yet found but last element is yet not checked 26 | if(temp->data==data){ 27 | return temp; 28 | } 29 | return NULL; 30 | } 31 | 32 | void print(node*head){ 33 | node*temp=head; 34 | while(temp->next!=head){ 35 | cout<data<<"-->"; 36 | temp = temp->next; 37 | } 38 | cout<data<<"-->"; 39 | cout<next=head; 51 | if(temp!=NULL){ 52 | while(temp->next!=head){ 53 | temp=temp->next; 54 | } 55 | temp->next=n; 56 | } 57 | else{ 58 | n->next=n; 59 | } 60 | head=n; 61 | } 62 | 63 | void deleteNode(node*&head, int data){ 64 | node*temp=head; 65 | node* del=getNode(head,data); 66 | 67 | if(del==NULL){ 68 | return; 69 | } 70 | 71 | //otherwise 72 | if(del==head){ 73 | head=head->next; 74 | } 75 | 76 | //stop one step behind the node to be deleted 77 | while(temp->next!=del){ 78 | temp=temp->next; 79 | } 80 | temp->next=del->next; 81 | delete del; 82 | } 83 | 84 | 85 | int main(){ 86 | node*head=NULL; 87 | 88 | insert(head,5); 89 | insert(head,4); 90 | insert(head,3); 91 | insert(head,2); 92 | insert(head,1); 93 | 94 | print(head); 95 | 96 | deleteNode(head,3); 97 | 98 | print(head); 99 | 100 | deleteNode(head,1); 101 | 102 | print(head); 103 | 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | //s stands for starting element 4 | // e stands for last element 5 | int merge(int *a,int s,int e) 6 | { 7 | int mid=(s+e)/2; 8 | int i=s; 9 | int j=mid+1; 10 | int k=s; 11 | 12 | int temp[100]; 13 | 14 | while(i<=mid && j<=e) 15 | { 16 | if(a[i]=e) 45 | { 46 | return 0; 47 | } 48 | int mid =(s+e)/2; 49 | mergesort(a,s,mid); 50 | mergesort(a,mid+1,e); 51 | 52 | //Merge the two parts 53 | merge(a,s,e); 54 | 55 | } 56 | int main() 57 | { 58 | int a[1000]; 59 | int n; 60 | cin>>n; 61 | for(int i=0;i>a[i]; 64 | } 65 | mergesort(a,0,n-1); 66 | for(int i=0;i 8 | using namespace std; 9 | 10 | int main(){ 11 | vector nums1{1,2,3,0,0,0}; 12 | int m = 3; 13 | vector nums2{2,5,6}; 14 | int n = 3; 15 | vector nums; 16 | 17 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* 5 | Take as input N, the size of array. Take N more inputs and store that in an array. Take as input M, the size of second array and take M more inputs and store that in second array. Write a function that returns the sum of two arrays. Print the value returned. 6 | 7 | Input Format--> 8 | Constraints-> 9 | Length of Array should be between 1 and 1000. 10 | 11 | Output Format--> 12 | Sample Input-> 13 | 4 14 | 1 0 2 9 15 | 5 16 | 3 4 5 6 7 17 | Sample Output-> 18 | 3, 5, 5, 9, 6, END 19 | Explanation-> 20 | Sum of [1, 0, 2, 9] and [3, 4, 5, 6, 7] is [3, 5, 5, 9, 6] and the first digit represents carry over , if any (0 here ) . 21 | */ 22 | 23 | int convert(vector v){ 24 | int n=v.size(); 25 | int res=0; 26 | int i=0; 27 | while(n>0){ 28 | res=res+(v[i]*pow(10,(n-1))); 29 | n--; 30 | i++; 31 | } 32 | return res; 33 | } 34 | 35 | void convertVec(int n){ 36 | vector v; 37 | while(n!=0){ 38 | int val=n%10; 39 | v.push_back(val); 40 | n=n/10; 41 | } 42 | 43 | for(int i=v.size()-1;i>=0;i--){ 44 | cout< v1; 52 | vector v2; 53 | 54 | cin>>n; 55 | 56 | for(int i=0;i>val; 59 | v1.push_back(val); 60 | } 61 | 62 | cin>>m; 63 | 64 | for(int i=0;i>val; 67 | v2.push_back(val); 68 | } 69 | 70 | int res=convert(v1)+convert(v2); 71 | 72 | convertVec(res); 73 | 74 | 75 | } 76 | -------------------------------------------------------------------------------- /Practice Problems/Container_with_most_water.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. 3 | 4 | Notice that you may not slant the container. 5 | 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | 13 | vector height{4,3,2,1,4}; 14 | int i=0; 15 | int j=height.size()-1; 16 | int water=INT_MIN; 17 | while(i 8 | using namespace std; 9 | 10 | int main(){ 11 | vector nums{5,7,7,8,8,10}; 12 | vector v; 13 | int target=8; 14 | 15 | bool present = binary_search(nums.begin(),nums.end(),target); 16 | int index_lb,index_ub; 17 | if(present){ 18 | auto lb=lower_bound(nums.begin(),nums.end(),target); 19 | index_lb=lb-nums.begin(); 20 | v.push_back(index_lb); 21 | 22 | auto ub=upper_bound(nums.begin(),nums.end(),target); 23 | index_ub=ub-nums.begin()-1; 24 | v.push_back(index_ub); 25 | } 26 | else{ 27 | index_lb=-1; 28 | index_ub=-1; 29 | v.push_back(index_lb); 30 | v.push_back(index_ub); 31 | } 32 | 33 | for(int i=0;i 9 | using namespace std; 10 | 11 | int main(){ 12 | 13 | vector nums1{1,2}; 14 | vector nums2{3,4}; 15 | vector v; 16 | for(int i=0;i 8 | using namespace std; 9 | 10 | int main(){ 11 | vector nums1{1,2,3,0,0,0}; 12 | int m = 3; 13 | vector nums2{2,5,6}; 14 | int n = 3; 15 | vector nums; 16 | 17 | for(int i=0;i 8 | using namespace std; 9 | 10 | 11 | bool isPalindrome(int x) { 12 | if(x<0){ 13 | return false; 14 | } 15 | if(x!=0 and x%10==0){ 16 | return false; 17 | } 18 | int reverse=0; 19 | while(x>reverse){ 20 | reverse=(reverse*10)+(x%10); 21 | x=x/10; 22 | } 23 | if(x==reverse){ 24 | return true; 25 | } 26 | if(x==reverse/10){ 27 | return true; 28 | } 29 | else{ 30 | return false; 31 | } 32 | } 33 | 34 | int main() { 35 | int x; 36 | cin>>x; 37 | cout< 8 | using namespace std; 9 | 10 | int main(){ 11 | vector nums{1}; 12 | int target=0; 13 | 14 | auto it=find(nums.begin(),nums.end(),target); 15 | int index=it-nums.begin(); 16 | 17 | if(index==nums.size()){ 18 | cout<<"-1"; 19 | } 20 | else{ 21 | cout< 6 | using namespace std; 7 | 8 | int main(){ 9 | vector nums{1,3,5,6}; 10 | int target=7; 11 | bool present=binary_search(nums.begin(),nums.end(),target); 12 | 13 | if(present){ 14 | auto it=find(nums.begin(),nums.end(),target); 15 | int index=it-nums.begin(); 16 | cout< 10 | using namespace std; 11 | 12 | int main(){ 13 | vector nums{2,0,2,1,1,0}; 14 | 15 | sort(nums.begin(),nums.end()); 16 | 17 | for(int i=0;i 8 | using namespace std; 9 | 10 | int main(){ 11 | vector nums{-4,-1,0,3,10}; 12 | vector v; 13 | 14 | for(int i=0;i 17 | 18 | using namespace std; 19 | 20 | int main(){ 21 | string str; 22 | cin>>str; 23 | int count=0; 24 | for(int i=0;icount){ 33 | count=count1; 34 | } 35 | } 36 | cout< 11 | using namespace std; 12 | 13 | int main(){ 14 | vector nums{1,2,3}; 15 | next_permutation(nums.begin(),nums.end()); 16 | for(int i=0;i 7 | using namespace std; 8 | 9 | int main(){ 10 | vector nums{0,0,1,1,1,2,2,3,3,4}; 11 | vector v; 12 | int count; 13 | for(int i=0;i 7 | using namespace std; 8 | 9 | int main(){ 10 | vector nums{0,1,2,2,3,0,4,2}; 11 | int val=2; 12 | int count=0; 13 | vector v; 14 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Queue{ 5 | int *arr; 6 | int f, r, cs, ms; 7 | 8 | public: 9 | Queue(int ds=5){ 10 | arr=new int[ds]; 11 | cs=0; 12 | ms=ds; 13 | f=0; 14 | r=ms-1; 15 | } 16 | 17 | bool full(){ 18 | return cs==ms; 19 | } 20 | 21 | bool empty(){ 22 | return cs == 0; 23 | } 24 | 25 | void push(int data){ 26 | if (!full()){ 27 | r = (r + 1) % ms; 28 | arr[r] = data; 29 | cs++; 30 | } 31 | } 32 | 33 | void pop() { 34 | if (!empty()){ 35 | f=(f+1)%ms; 36 | cs--; 37 | } 38 | } 39 | 40 | int front(){ 41 | return arr[f]; 42 | } 43 | 44 | ~Queue(){ 45 | if (arr!=NULL){ 46 | delete [] arr; 47 | arr=NULL; 48 | } 49 | } 50 | }; 51 | 52 | int main() { 53 | Queue q; 54 | 55 | for (int i=1;i<=6;i++) { 56 | q.push(i); 57 | } 58 | 59 | q.pop(); 60 | q.pop(); 61 | 62 | q.push(7); 63 | 64 | while(!q.empty()){ 65 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class Queue{ 6 | int cs; 7 | list l; 8 | 9 | public: 10 | Queue(){ 11 | cs=0; 12 | } 13 | 14 | bool isEmpty(){ 15 | return cs==0; 16 | } 17 | 18 | void push(int data){ 19 | l.push_back(data); 20 | cs++; 21 | } 22 | 23 | void pop(){ 24 | if(!isEmpty()){ 25 | cs--; 26 | l.pop_front(); 27 | } 28 | } 29 | 30 | int front(){ 31 | return l.front(); 32 | } 33 | }; 34 | 35 | int main() { 36 | Queue q; 37 | 38 | for (int i=1;i<=10;i++) { 39 | q.push(i); 40 | } 41 | 42 | while(!q.isEmpty()){ 43 | cout<hacktoberfest page 19 | 20 | ### Hall of Fame (Check out Contributors) 🏆🦸 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | #### DON'T FORGET TO DROP A STAR IF YOU LIKE MY WORK :) 29 | -------------------------------------------------------------------------------- /Rabinkarp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define endl '\n' 4 | #define ll long long 5 | #define mod 1000000007 6 | ll dp[100001]; 7 | void init(string s) 8 | { 9 | ll value=0; 10 | ll p=31; 11 | ll power=1; 12 | dp[0]=(s[0]-'a'+1); 13 | for(int i=1;i0) result=(result-dp[l-1]+mod)%mod; // Important line if result get negative we have to add mod 35 | return result%mod; 36 | } 37 | int main() 38 | { 39 | ios_base::sync_with_stdio(false); 40 | cin.tie(NULL); 41 | string input; 42 | getline(cin,input); 43 | init(input); 44 | string patt; 45 | getline(cin,patt); 46 | int ps=int(patt.size()); 47 | int is=int(input.size()); 48 | if(ps>is) 49 | { 50 | cout<<"Invalid input"< 2 | #include 3 | #include 4 | using namespace std; 5 | void display(int *array, int size) { 6 | for(int i = 0; i pocket[10]; //radix of decimal number is 10 13 | for(i = 0; i< max; i++) { 14 | m = pow(10, i+1); 15 | p = pow(10, i); 16 | for(j = 0; j> n; 36 | cout << "Enter the maximum digit of elements: "; 37 | cin >> max; 38 | int arr[n]; //create an array with given number of elements 39 | cout << "Enter elements:" << endl; 40 | for(int i = 0; i> arr[i]; 42 | } 43 | cout << "Data before Sorting: "; 44 | display(arr, n); 45 | radixSort(arr, n, max); 46 | cout << "Data after Sorting: "; 47 | display(arr, n); 48 | } 49 | -------------------------------------------------------------------------------- /Recurssion/Array_is_sorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSorted(int a[], int n){ 5 | //base case 6 | if(n==0 || n==1){ 7 | return true; 8 | } 9 | //recursive case 10 | if(a[0] 2 | using namespace std; 3 | 4 | int fact(int n){ 5 | //base case 6 | if(n==0){ 7 | return 1; 8 | } 9 | //recursive case 10 | return n*fact(n-1); 11 | } 12 | 13 | 14 | int main() { 15 | int n; 16 | cin>>n; 17 | 18 | cout< 2 | using namespace std; 3 | 4 | int fibo(int n){ 5 | //base case 6 | if(n==0 || n==1){ 7 | return n; 8 | } 9 | //recursive case 10 | return fibo(n-1)+fibo(n-2); 11 | } 12 | 13 | 14 | int main() { 15 | int n; 16 | cin>>n; 17 | 18 | cout< 2 | using namespace std; 3 | 4 | int firstOcc(int *a, int n, int key){ 5 | //base case 6 | if(n==0){ 7 | return -1; 8 | } 9 | //recursive case 10 | if(a[0]==key){ 11 | return 0; 12 | } 13 | int i = firstOcc(a+1,n-1,key); 14 | if(i==-1){ 15 | return -1; 16 | } 17 | return i+1; 18 | } 19 | 20 | 21 | //Different Style of writing same function 22 | int linearSearch(int *a, int n, int key){ 23 | //base case 24 | if(i==n){ // In for-loop -> Stopping Criteria 25 | return -1; 26 | } 27 | //recursive case 28 | if(a[i]==key){ // In for-loop -> If condition inside loop 29 | return i; 30 | } 31 | return linearSearch(int *a, int n+1, int key); // In for-loop -> Update Condition 32 | 33 | /* The above code will work like a for loop*/ 34 | } 35 | 36 | 37 | int main() { 38 | int a[]={1,2,3,7,4,5,6,7,10}; 39 | int key=7; 40 | int n=sizeof(a)/sizeof(int); 41 | 42 | cout< 2 | using namespace std; 3 | 4 | void dec(int n){ 5 | //base case 6 | if(n==0){ 7 | return; 8 | } 9 | //recursive case 10 | cout<>n; 27 | 28 | cout<<"Decreasing Order : "; 29 | dec(n); 30 | cout< 2 | using namespace std; 3 | 4 | int lastOcc(int *a, int n, int key){ 5 | //base case 6 | if(n==0){ 7 | return -1; 8 | } 9 | //recursive case 10 | int i = lastOcc(a+1,n-1,key); 11 | if(i==-1){ 12 | if(a[0]==key){ 13 | return 0; 14 | } 15 | else{ 16 | return -1; 17 | } 18 | } 19 | //Otherwise if i returned by subproblem is not -1 20 | return i+1; 21 | } 22 | 23 | 24 | int main() { 25 | int a[]={1,2,3,7,4,5,6,7,10}; 26 | int key=7; 27 | int n=sizeof(a)/sizeof(int); 28 | 29 | cout< 4 | using namespace std; 5 | 6 | string reverseWords(string s) 7 | { 8 | // code here 9 | int k=s.length(); 10 | string res=""; 11 | for(int i=k-1;i>=0;i--) 12 | { 13 | if(s[i]=='.') 14 | { 15 | for(int j=i+1;j 2 | #include 3 | using namespace std; 4 | 5 | void transfer(stack &s1,stack &s2,int n){ 6 | for (int i=0;i &s1){ 14 | // In final stack, the topmost element should be at the bottom 15 | int n=s1.size(); 16 | //helper stack 17 | stack s2; 18 | 19 | for(int i=0;i s; 39 | s.push(1); 40 | s.push(2); 41 | s.push(3); 42 | s.push(4); 43 | s.push(5); 44 | 45 | reverseStack(s); 46 | while(!s.empty()){ 47 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | void swap(int *a, int *b) 7 | { 8 | int t = *a; 9 | *a = *b; 10 | *b = t; 11 | } 12 | 13 | void sort(int* arr, int n) 14 | { 15 | int i, j; 16 | int minIndex = -1; 17 | 18 | for (i = 0; i < n-1; i++) 19 | { 20 | minIndex = i; 21 | for (j = i+1; j < n; j++) 22 | if (arr[j] < arr[minIndex]) 23 | minIndex = j; 24 | 25 | swap(&arr[minIndex], &arr[i]); 26 | } 27 | } 28 | 29 | void printArray(int arr[], int size) 30 | { 31 | for (int i=0 ; i < size ; i++) 32 | cout << arr[i] << " "; 33 | cout << endl; 34 | } 35 | 36 | int main() 37 | { 38 | int arr[] = {80, 15, 19, 90, 11, 13, 32}; 39 | int n = sizeof(arr)/sizeof(arr[0]); 40 | 41 | sort(arr, n); 42 | printArray(arr, n); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /ShellSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* function to sort arr using shellSort */ 5 | int shellSort(int arr[], int n) 6 | { 7 | // Start with a big gap, then reduce the gap 8 | for (int gap = n/2; gap > 0; gap /= 2) 9 | { 10 | // Do a gapped insertion sort for this gap size. 11 | // The first gap elements a[0..gap-1] are already in gapped order 12 | // keep adding one more element until the entire array is 13 | // gap sorted 14 | for (int i = gap; i < n; i += 1) 15 | { 16 | // add a[i] to the elements that have been gap sorted 17 | // save a[i] in temp and make a hole at position i 18 | int temp = arr[i]; 19 | 20 | // shift earlier gap-sorted elements up until the correct 21 | // location for a[i] is found 22 | int j; 23 | for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) 24 | arr[j] = arr[j - gap]; 25 | 26 | // put temp (the original a[i]) in its correct location 27 | arr[j] = temp; 28 | } 29 | } 30 | return 0; 31 | } 32 | 33 | void printArray(int arr[], int n) 34 | { 35 | for (int i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Bubble Sort 7 | void bubble_sort(int a[], int n){ 8 | //N-1 large elements to the end 9 | for(int i=1;i<=n-1;i++){ 10 | 11 | //Pairwise Swapping in the unsorted part of the array 12 | for(int j=0;j<=(n-i-1);j++){ 13 | if(a[j]>a[j+1]){ 14 | swap(a[j],a[j+1]); 15 | } 16 | } 17 | } 18 | } 19 | 20 | int main(){ 21 | 22 | int n, key; 23 | cout<<"Enter size of array"<>n; 25 | 26 | int a[100000]; 27 | 28 | //Create a Reverse Sorted Array 29 | cout<<"Enter elements of array"< 8 | using namespace std; 9 | 10 | int main(){ 11 | vector nums{-4,-1,0,3,10}; 12 | vector v; 13 | 14 | for(int i=0;i 2 | class stackusingarray{ 3 | int *data; 4 | int nextIndex; 5 | int capacity; 6 | public: 7 | stackusingarray(int totalsize){ 8 | data = new int[totalsize]; 9 | nextIndex = 0; 10 | capacity = totalsize; 11 | } 12 | // check for empty 13 | bool isEmpty(){ 14 | return nextIndex ==0; 15 | } 16 | // check for size 17 | int size(){ 18 | return nextIndex+1; 19 | } 20 | //push funxction 21 | void push(int element){ 22 | if(nextIndex == capacity){ 23 | cout << "Stacki is full" << endl; 24 | return ; 25 | } 26 | data[nextIndex++] = element ; 27 | } 28 | //pop function 29 | int pop(){ 30 | if(isEmpty()){ 31 | cout << "Stacki is Empty" << endl; 32 | return INT_MIN; 33 | } 34 | nextIndex--; 35 | return datap[nextIndex+1]; 36 | } 37 | //top element 38 | int top(){ 39 | if(isEmpty()){ 40 | cout << "Stacki is Empty" << endl; 41 | return INT_MIN; 42 | } 43 | return data[nextIndex-1]; 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Stacks/Reverse_Stack_Using_1_Extra_Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void transfer(stack &s1,stack &s2,int n){ 6 | for (int i=0;i &s1){ 14 | // In final stack, the topmost element should be at the bottom 15 | int n=s1.size(); 16 | //helper stack 17 | stack s2; 18 | 19 | for(int i=0;i s; 39 | s.push(1); 40 | s.push(2); 41 | s.push(3); 42 | s.push(4); 43 | s.push(5); 44 | 45 | reverseStack(s); 46 | while(!s.empty()){ 47 | cout< 2 | using namespace std; 3 | 4 | class StackNode { 5 | public: 6 | int data; 7 | StackNode *next; 8 | 9 | StackNode(int data) 10 | { 11 | this->data = data; 12 | this->next = NULL; 13 | } 14 | }; 15 | 16 | class Stack { 17 | 18 | StackNode *top; 19 | 20 | public: 21 | 22 | // Push and pop operations 23 | void push(int data) 24 | { 25 | if (top == NULL) { 26 | top = new StackNode(data); 27 | return; 28 | } 29 | StackNode *s = new StackNode(data); 30 | s->next = top; 31 | top = s; 32 | } 33 | 34 | StackNode* pop() 35 | { 36 | StackNode *s = top; 37 | top = top->next; 38 | return s; 39 | } 40 | 41 | // prints contents of stack 42 | void display() 43 | { 44 | StackNode *s = top; 45 | while (s != NULL) { 46 | cout << s->data << " "; 47 | s = s->next; 48 | } 49 | cout << endl; 50 | } 51 | 52 | // Reverses the stack using simple 53 | // linked list reversal logic. 54 | void reverse() 55 | { 56 | StackNode *prev, *cur, *succ; 57 | cur = prev = top; 58 | cur = cur->next; 59 | prev->next = NULL; 60 | while (cur != NULL) { 61 | 62 | succ = cur->next; 63 | cur->next = prev; 64 | prev = cur; 65 | cur = succ; 66 | } 67 | top = prev; 68 | } 69 | }; 70 | 71 | // driver code 72 | int main() 73 | { 74 | Stack *s = new Stack(); 75 | s->push(1); 76 | s->push(2); 77 | s->push(3); 78 | s->push(4); 79 | cout << "Original Stack" << endl;; 80 | s->display(); 81 | cout << endl; 82 | 83 | // reverse 84 | s->reverse(); 85 | 86 | cout << "Reversed Stack" << endl; 87 | s->display(); 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Stacks/stack_implementation_using_vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | //Implementation of Stack Data Structure using Vector 6 | 7 | class Stack{ 8 | private: 9 | vector v; 10 | public: 11 | void push(int data){ 12 | v.push_back(data); 13 | } 14 | bool empty(){ 15 | return v.size()==0; 16 | } 17 | void pop(){ 18 | if(!empty()){ 19 | v.pop_back(); 20 | } 21 | } 22 | int top(){ 23 | return v[v.size()-1]; 24 | } 25 | }; 26 | 27 | int main() { 28 | Stack s; 29 | 30 | for(int i=1;i<=5;i++){ 31 | s.push(i*i); 32 | } 33 | //Try to print the content of the stack by popping each element 34 | while(!s.empty()){ 35 | cout< 2 | #include 3 | using namespace std; 4 | 5 | //Implementation of Stack Data Structure using Vector 6 | 7 | template 8 | 9 | class Stack{ 10 | private: 11 | vector v; 12 | public: 13 | void push(T data){ 14 | v.push_back(data); 15 | } 16 | bool empty(){ 17 | return v.size()==0; 18 | } 19 | void pop(){ 20 | if(!empty()){ 21 | v.pop_back(); 22 | } 23 | } 24 | T top(){ 25 | return v[v.size()-1]; 26 | } 27 | }; 28 | 29 | int main() { 30 | Stack s; 31 | 32 | for(int i=65;i<=70;i++){ 33 | s.push(i); 34 | } 35 | //Try to print the content of the stack by popping each element 36 | while(!s.empty()){ 37 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | 7 | stack s; 8 | 9 | for(int i=0;i<5;i++){ 10 | s.push(i); 11 | } 12 | 13 | while(!s.empty()){ 14 | cout< 2 | int ternarySearch(int l, int r, int key, int ar[]) 3 | { 4 | if (r >= l) { 5 | int mid1 = l + (r - l) / 3; 6 | int mid2 = r - (r - l) / 3; 7 | if (ar[mid1] == key) { 8 | return mid1; 9 | } 10 | if (ar[mid2] == key) { 11 | return mid2; 12 | } 13 | if (key < ar[mid1]) { 14 | 15 | return ternarySearch(l, mid1 - 1, key, ar); 16 | } 17 | else if (key > ar[mid2]) { 18 | 19 | return ternarySearch(mid2 + 1, r, key, ar); 20 | } 21 | else { 22 | 23 | return ternarySearch(mid1 + 1, mid2 - 1, key, ar); 24 | } 25 | } 26 | 27 | return -1; 28 | } 29 | 30 | int main() 31 | { 32 | int l, r, p, key; 33 | int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 34 | 35 | l = 0; 36 | r = 9; 37 | key = 5; 38 | 39 | p = ternarySearch(l, r, key, ar); 40 | 41 | printf("Index of %d is %d\n", key, p); 42 | key = 50; 43 | p = ternarySearch(l, r, key, ar); 44 | 45 | printf("Index of %d is %d", key, p); 46 | } 47 | -------------------------------------------------------------------------------- /Vectors/car_sorting_prob_using_vector_and_class.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Car{ 8 | public: 9 | string car_name; 10 | int x,y; 11 | Car(){ 12 | 13 | } 14 | Car(string n, int x, int y){ 15 | car_name=n; 16 | this->x=x; 17 | this->y=y; 18 | } 19 | int distance(){ 20 | //returns square of distance from origin 21 | return x*x+y*y; 22 | } 23 | }; 24 | 25 | bool compare(Car A, Car B){ 26 | int da=A.distance(); 27 | int db=B.distance(); 28 | if(da==db){ 29 | return A.car_name.length()>n; 37 | string name; 38 | vector v; 39 | for(int i=0;i>name>>x>>y; 41 | Car temp(name,x,y); 42 | v.push_back(temp); 43 | } 44 | 45 | sort(v.begin(),v.end(),compare); 46 | 47 | for(auto c:v){ 48 | cout<<"Car Name: "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool compare(pair p1, pair p2){ 7 | int d1=p1.first*p1.first+p1.second*p1.second; 8 | int d2=p2.first*p2.first+p2.second*p2.second; 9 | 10 | //If two cars have same distance then sort according to x coordinate 11 | if(d1==d2){ 12 | return p1.first>n; 21 | 22 | vector> v; 23 | for(int i=0;i>x>>y; 25 | v.push_back(make_pair(x,y)); 26 | } 27 | 28 | sort(v.begin(),v.end(),compare); 29 | 30 | for(auto p:v){ 31 | cout<<"Car "< 2 | using namespace std; 3 | 4 | void addedge(vector> &adj,int u,int v) 5 | { 6 | adj[u].push_back(v); 7 | adj[v].push_back(u); 8 | } 9 | 10 | void printgraph(vector>&adj) 11 | { 12 | int i=0; 13 | for(auto x: adj) 14 | { 15 | cout< "; 16 | for( auto l:x) 17 | { 18 | cout<< l<<" | "; 19 | } 20 | cout<>adj(v); 28 | addedge(adj,0,1); 29 | addedge(adj,0,2); 30 | addedge(adj,1,2); 31 | addedge(adj,1,3); 32 | printgraph(adj); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /array deletion: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 5 | 6 | 7 | int main() { 8 | int list[50],n,temp,i,j,position; 9 | printf("enter number of elements you want to enter: "); 10 | scanf("%d",&n); 11 | 12 | printf("entering the elements:"); 13 | for(i=1;i<=n;i++) 14 | { 15 | scanf("%d",list[i]); 16 | } 17 | printf("displaying original list "); 18 | for(i=1;i<=n;i++) 19 | { 20 | printf("%d",list[i]); 21 | } 22 | printf("enter the position from which you want to delete element"); 23 | scanf("%d",&position); 24 | 25 | temp=list[position]; 26 | j=n-1; 27 | while(1) 28 | { 29 | if(j<=n) 30 | { 31 | list[position]=list[position+1]; 32 | position++; 33 | 34 | } 35 | j++; 36 | } 37 | printf("new list is:"); 38 | for(i=1;i>=j;i++) 39 | { 40 | printf("%d",list[i]); 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /arrayInsertionUsingLinkedList: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | struct Node { 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | void insertelements(Node* root, int item) 11 | { 12 | Node* temp = new Node; 13 | Node* ptr; 14 | temp->data = item; 15 | temp->next = NULL; 16 | 17 | if (*root == NULL) 18 | *root = temp; 19 | else { 20 | ptr = *root; 21 | while (ptr->next != NULL) 22 | ptr = ptr->next; 23 | ptr->next = temp; 24 | } 25 | } 26 | 27 | void display(Node* root) 28 | { 29 | while (root != NULL) { 30 | cout << root->data << " "; 31 | root = root->next; 32 | } 33 | } 34 | 35 | Node *arrayToList(int list[], int length) 36 | { 37 | Node *root = NULL; 38 | for (int i = 0; i < length; i++) 39 | insertelements(&root, list[i]); 40 | return root; 41 | } 42 | 43 | 44 | int main() 45 | { 46 | int list[] = { 21, 32, 43, 54, 65 }; 47 | int length = sizeof(arr) / sizeof(arr[0]); 48 | Node* root = arrayToList(list, length); 49 | display(root); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /bellmanford algorithm: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct edge 4 | { 5 | int source; 6 | int dest; 7 | int weight; 8 | }; 9 | main() 10 | { 11 | int v,i; 12 | cout<<"enter no of vertices\n"; 13 | cin>>v; 14 | int e; 15 | cout<<"enter no of edges\n"; 16 | cin>>e; 17 | edge arr[e]; 18 | int key[v]; 19 | for(i=0;i>arr[i].source; 29 | cout<<"enter dest vertex\n"; 30 | cin>>arr[i].dest; 31 | cout<<"enter weight\n"; 32 | cin>>arr[i].weight; 33 | } 34 | for(i=0;ikey[arr[j].source]+arr[j].weight) 39 | { 40 | key[arr[j].dest]=key[arr[j].source]+arr[j].weight; 41 | } 42 | } 43 | } 44 | cout<<"after relaxing\n"; 45 | for(i=0;i 5 | using namespace std; 6 | 7 | bool bipartiteDfs(int node, vector adj[], int color[]) { 8 | for(auto it : adj[node]) { 9 | if(color[it] == -1) { 10 | color[it] = 1 - color[node]; 11 | if(!bipartiteDfs(it, adj, color)) { 12 | return false; 13 | } 14 | } else if(color[it] == color[node]) return false; 15 | } 16 | return true; 17 | } 18 | bool checkBipartite(vector adj[], int n) { 19 | int color[n]; 20 | memset(color, -1, sizeof color); 21 | for(int i = 0;i> n >> m; 37 | vector adj[n]; 38 | for(int i = 0;i> u >> v; 41 | adj[u].push_back(v); 42 | adj[v].push_back(u); 43 | } 44 | 45 | if(checkBipartite(adj, n)) { 46 | cout << "yes"; 47 | } else { 48 | cout << "No"; 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /buzznumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class buzznumber 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter a number"); 8 | int n= sc.nextInt(); 9 | int r=n%10; 10 | int d=n%7; 11 | if(r==7 || d==0) 12 | System.out.println(n+" is a Buzz Number"); 13 | else 14 | System.out.println(n+" is not a Buzz Number"); 15 | } 16 | } -------------------------------------------------------------------------------- /calculator_tk.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | class MyWindow: 3 | def __init__(self, win): 4 | self.lbl1=Label(win, text='First number') 5 | self.lbl2=Label(win, text='Second number') 6 | self.lbl3=Label(win, text='Result') 7 | self.t1=Entry(bd=3) 8 | self.t2=Entry() 9 | self.t3=Entry() 10 | self.btn1 = Button(win, text='Add') 11 | self.btn2=Button(win, text='Subtract') 12 | self.lbl1.place(x=100, y=50) 13 | self.t1.place(x=200, y=50) 14 | self.lbl2.place(x=100, y=100) 15 | self.t2.place(x=200, y=100) 16 | self.b1=Button(win, text='Add', command=self.add) 17 | self.b2=Button(win, text='Subtract') 18 | self.b2.bind('', self.sub) 19 | self.b1.place(x=100, y=150) 20 | self.b2.place(x=200, y=150) 21 | self.lbl3.place(x=100, y=200) 22 | self.t3.place(x=200, y=200) 23 | def add(self): 24 | self.t3.delete(0, 'end') 25 | num1=int(self.t1.get()) 26 | num2=int(self.t2.get()) 27 | result=num1+num2 28 | self.t3.insert(END, str(result)) 29 | def sub(self, event): 30 | self.t3.delete(0, 'end') 31 | num1=int(self.t1.get()) 32 | num2=int(self.t2.get()) 33 | result=num1-num2 34 | self.t3.insert(END, str(result)) 35 | 36 | window=Tk() 37 | mywin=MyWindow(window) 38 | window.title('Hello Python') 39 | window.geometry("400x300+10+10") 40 | window.mainloop() 41 | -------------------------------------------------------------------------------- /catalan_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int catalan(int n){ 5 | if(n<=1) 6 | return 1; 7 | int res=0; 8 | for(int i=0; i 3 | using namespace std; 4 | 5 | // To find gap between elements 6 | int getNextGap(int gap) 7 | { 8 | // Shrink gap by Shrink factor 9 | gap = (gap*10)/13; 10 | 11 | if (gap < 1) 12 | return 1; 13 | return gap; 14 | } 15 | 16 | // Function to sort a[0..n-1] using Comb Sort 17 | void combSort(int a[], int n) 18 | { 19 | // Initialize gap 20 | int gap = n; 21 | 22 | // Initialize swapped as true to make sure that loop runs 23 | bool swapped = true; 24 | 25 | // Keep running while gap is more than 1 and last iteration caused a swap 26 | while (gap != 1 || swapped == true) 27 | { 28 | // Find next gap 29 | gap = getNextGap(gap); 30 | 31 | // Initialize swapped as false so that we can check if swap happened or not 32 | swapped = false; 33 | 34 | // Compare all elements with current gap 35 | for (int i=0; i a[i+gap]) 38 | { 39 | swap(a[i], a[i+gap]); 40 | swapped = true; 41 | } 42 | } 43 | } 44 | } 45 | 46 | int main() 47 | { 48 | int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0}; 49 | int n = sizeof(a)/sizeof(a[0]); 50 | 51 | combSort(a, n); 52 | 53 | printf("Sorted array: \n"); 54 | for (int i=0; i 2 | using namespace std; 3 | vector adj[1000]; 4 | int num_v = 0; 5 | void insertion_in_adj_list() 6 | { 7 | int n; 8 | cin >> n; 9 | while (n--) 10 | { 11 | int a, b; 12 | cin >> a >> b; 13 | adj[a].push_back(b); 14 | } 15 | } 16 | 17 | void dfs_rec(int num_of_v,int start,vector &visited) 18 | { 19 | visited[start]=true; 20 | cout< visited(num_of_v, false); 30 | dfs_rec(num_of_v,start,visited); 31 | //above is enough to get all the possible visitable vertices from start 32 | 33 | for(int i=0;i 2 | using namespace std; 3 | 4 | int fact(int n){ 5 | //base case 6 | if(n==0){ 7 | return 1; 8 | } 9 | //recursive case 10 | return n*fact(n-1); 11 | } 12 | 13 | 14 | int main() { 15 | int n; 16 | cin>>n; 17 | 18 | cout< 2 | int main() { 3 | int i, n, t1 = 0, t2 = 1, nextTerm; 4 | printf("Enter the number of terms: "); 5 | scanf("%d", &n); 6 | printf("Fibonacci Series: "); 7 | 8 | for (i = 1; i <= n; ++i) { 9 | printf("%d, ", t1); 10 | nextTerm = t1 + t2; 11 | t1 = t2; 12 | t2 = nextTerm; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /firstrepeatingarrayelement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class FirstRepeatingElement 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter the number of elements of your array"); 8 | int m= sc.nextInt(); 9 | int arr[] = new int[m]; 10 | System.out.println("Enter the elements of your array"); 11 | for(int i=0;i1) 24 | System.out.println("The first repeating element is "+arr[i]); 25 | System.out.println(arr[i]+" occurs "+n+" times in the entered array"); 26 | break; 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /floyd warshall algorithm: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define p 1000000 4 | main() 5 | { 6 | long long int i,j,k,n; 7 | cout<<"enter no of vertices\n"; 8 | cin>>n; 9 | long long int a[n][n],b[n][n],arr[n][n]; 10 | cout<<"enter matrices\n"; 11 | for(i=0;i>arr[i][j]; 15 | } 16 | } 17 | for(i=0;i 2 | #include 3 | #define N 8 4 | #define INF 9999999 5 | int Flow[N][N]; 6 | int visited[N]; 7 | int min(int a, int b); 8 | int dfs(int s, int t, int minimum); 9 | 10 | int graph[N][N] = { {0, 2, 0, 0, 0, 0, 0, 4}, 11 | {0, 0, 0, 2, 0, 0, 0, 3}, 12 | {0, 0, 0, 4, 3, 0, 0, 0}, 13 | {5, 0, 0, 0, 0, 0, 0, 0}, 14 | {0, 0, 0, 0, 0, 4, 0, 0}, 15 | {0, 0, 0, 2, 0, 0, 0, 3}, 16 | {3, 0, 5, 0, 2, 0, 0, 0}, 17 | {0, 0, 0, 0, 0, 0, 0, 0} }; 18 | 19 | 20 | int main() { 21 | int sent; 22 | memset(Flow, 0, sizeof(Flow)); 23 | memset(visited, 0, sizeof(visited)); 24 | int s = 6; 25 | int t = 7; 26 | int max_flow = 0; 27 | while (sent = dfs(s, t, INF)) { 28 | max_flow += sent; 29 | memset(visited, 0, sizeof(visited)); 30 | } 31 | printf("The max flow from source to sink is %d", max_flow); 32 | printf("\n"); 33 | } 34 | 35 | int min(int a, int b) 36 | { 37 | if(a 0) { 56 | if (sent = dfs (i, t, min(minimum, flow_capacity))) { 57 | Flow[s][i] += sent; 58 | Flow[i][s] -= sent; 59 | return sent; 60 | } 61 | } 62 | } 63 | return 0; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /heap sort: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | void heap1(int list[], int number, int i) 7 | { 8 | int largest = i; 9 | int left = 2*i + 1; 10 | int right = 2*i + 2; 11 | 12 | 13 | if (left < number && list[left] > list[largest]) 14 | largest = left; 15 | 16 | 17 | if (right < number && list[right] > list[largest]) 18 | largest = right; 19 | 20 | // If largest is not root 21 | if (largest != i) 22 | { 23 | swap(list[i], list[largest]); 24 | 25 | 26 | heap1(list, number, largest); 27 | } 28 | } 29 | 30 | void heapSort(int list[], int number) 31 | { 32 | for (int i = number / 2 - 1; i >= 0; i--) 33 | heap1(list, number, i); 34 | 35 | 36 | for (int i=number-1; i>0; i--) 37 | { 38 | 39 | swap(list[0], list[i]); 40 | heap(list, i, 0); 41 | } 42 | } 43 | 44 | void printArray(int list[], int number) 45 | { 46 | for (int i=0; i 2 | int main() 3 | { 4 | char hexNum[100]; 5 | long int count=0; 6 | printf("Enter a hexadecimal number To Convet it into Binary : "); 7 | scanf("%s",hexNum); 8 | printf("\nBinary Number is : "); 9 | while(hexNum[count]) 10 | { 11 | switch(hexNum[count]) 12 | { 13 | case '0' : printf("0000"); 14 | break; 15 | case '1' : printf("0001"); 16 | break; 17 | case '2' : printf("0010"); 18 | break; 19 | case '3' : printf("0011"); 20 | break; 21 | case '4' : printf("0100"); 22 | break; 23 | case '5' : printf("0101"); 24 | break; 25 | case '6' : printf("0110"); 26 | break; 27 | case '7' : printf("0111"); 28 | break; 29 | case '8' : printf("1000"); 30 | break; 31 | case '9' : printf("1001"); 32 | break; 33 | case 'A' : printf("1010"); 34 | break; 35 | case 'B' : printf("1011"); 36 | break; 37 | case 'C' : printf("1100"); 38 | break; 39 | case 'D' : printf("1101"); 40 | break; 41 | case 'E' : printf("1110"); 42 | break; 43 | case 'F' : printf("1111"); 44 | break; 45 | case 'a' : printf("1010"); 46 | break; 47 | case 'b' : printf("1011"); 48 | break; 49 | case 'c' : printf("1100"); 50 | break; 51 | case 'd' : printf("1101"); 52 | break; 53 | case 'e' : printf("1110"); 54 | break; 55 | case 'f' : printf("1111"); 56 | break; 57 | default : printf("\nInvalid Entry, Please Try Again %c",hexNum[count]); 58 | } 59 | count++; 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /iteratorHashmap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | unordered_map ourmap; 10 | ourmap["abc"]=1; 11 | ourmap["abc1"]=2; 12 | ourmap["abc2"]=3; 13 | ourmap["abc3"]=4; 14 | ourmap["abc4"]=5; 15 | ourmap["abc5"]=6; 16 | 17 | //syntax of iterator in unordered map; 18 | unordered_map::iterator it=ourmap.begin(); //it is a pointer which is pointing to ourmap.begin() 19 | while(it!=ourmap.end()) 20 | { 21 | cout<<"key : "<first<<" value: "<second<v; 29 | v.push_back(10); 30 | v.push_back(9); 31 | v.push_back(8); 32 | v.push_back(7); 33 | v.push_back(6); 34 | 35 | vector::iterator it1=v.begin(); 36 | while(it1!=v.end()) 37 | { 38 | cout<<*it1< 2 | using namespace std; 3 | 4 | void fill_lps(string str, vector&lps) 5 | { 6 | int len=0; 7 | int n=str.size(); 8 | lps[0]=0; 9 | int i=1; 10 | while(i0 26 | { 27 | len=lps[len-1]; 28 | } 29 | } 30 | } 31 | } 32 | 33 | void kmp(string pat,string txt) 34 | { 35 | int n=txt.length(); 36 | int m=pat.length(); 37 | vectorlps(m); 38 | fill_lps(pat,lps); 39 | int i=0,j=0; 40 | while(i 2 | using namespace std; 3 | 4 | 5 | struct edge{ 6 | int start,end,weight; 7 | bool operator<(edge const& other) { 8 | return weight < other.weight; 9 | } 10 | }; 11 | int n; 12 | vector edges; 13 | vector result; 14 | void make_set(int v,vector& parent) { 15 | parent[v] = v; 16 | } 17 | int find_parent(int v,vector& parent){ 18 | if (v == parent[v]){ 19 | return v; 20 | } 21 | return find_parent(parent[v],parent); 22 | } 23 | 24 | void union_find(int a,int b,vector& parent){ 25 | a = find_parent(a,parent); 26 | b = find_parent(b,parent); 27 | if(a != b){ 28 | parent[b] = a; 29 | } 30 | } 31 | 32 | void kruskal(){ 33 | //make_parent(); 34 | vector parent(n); 35 | 36 | for (int i = 1; i <= n; i++){ 37 | make_set(i,parent); 38 | } 39 | int cost = 0,count = 0; 40 | sort( edges.begin(),edges.end() ); 41 | for(auto e : edges){ 42 | if( find_parent(e.start,parent) != find_parent(e.end,parent) ){ 43 | cost += e.weight; 44 | result.push_back(e); 45 | union_find(e.start, e.end,parent); 46 | count++; 47 | } 48 | if(count == n-1){ 49 | break; 50 | } 51 | } 52 | 53 | cout << cost << endl; 54 | } 55 | 56 | int main(){ 57 | 58 | int e; 59 | cin >> n >> e; 60 | for(int i=1;i<=e ;i++){ 61 | int start , end , weight; 62 | cin >> start >> end >> weight; 63 | edges.push_back({start,end,weight}); 64 | } 65 | kruskal(); 66 | 67 | } 68 | -------------------------------------------------------------------------------- /last_occurance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lastOcc(int *a, int n, int key){ 5 | //base case 6 | if(n==0){ 7 | return -1; 8 | } 9 | //recursive case 10 | int i = lastOcc(a+1,n-1,key); 11 | if(i==-1){ 12 | if(a[0]==key){ 13 | return 0; 14 | } 15 | else{ 16 | return -1; 17 | } 18 | } 19 | //Otherwise if i returned by subproblem is not -1 20 | return i+1; 21 | } 22 | 23 | 24 | int main() { 25 | int a[]={1,2,3,7,4,5,6,7,10}; 26 | int key=7; 27 | int n=sizeof(a)/sizeof(int); 28 | 29 | cout< 2 | using namespace std; 3 | 4 | #define V 5 5 | 6 | int minKey(int key[], bool mstSet[]) 7 | { 8 | 9 | int min = INT_MAX, min_index; 10 | 11 | for (int v = 0; v < V; v++) 12 | if (mstSet[v] == false && key[v] < min) 13 | min = key[v], min_index = v; 14 | 15 | return min_index; 16 | } 17 | 18 | 19 | void printMST(int parent[], int graph[V][V]) 20 | { 21 | cout<<"Edge \tWeight\n"; 22 | for (int i = 1; i < V; i++) 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | priority_queuep; //bydefault is max heap 8 | p.push(12); 9 | p.push(16); 10 | p.push(19); 11 | p.push(160); 12 | p.push(100); 13 | cout<<"size: "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // inbuilt max heap is converted to inbuilt mi heap 9 | priority_queue,greater>p; 10 | p.push(120); 11 | p.push(16); 12 | p.push(19); 13 | p.push(160); 14 | p.push(100); 15 | cout<<"size: "< 2 | #include 3 | using namespace std; 4 | 5 | void ksmallestelement(int *arr,int n, int k) 6 | { 7 | priority_queue min1; 8 | for(int i=0;iarr[i]) 15 | { 16 | int temp=min1.top(); 17 | min1.push(arr[i]); 18 | arr[i]=temp; 19 | } 20 | } 21 | while(min1.empty()) 22 | { 23 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void ksortedArray(int *arr,int n,int k) //time complexity O(n) 6 | { 7 | priority_queuepq; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | //insertHeapusingArray is a combination of insert and remove 5 | void insertHeapusingArray(int arr[],int n) 6 | { 7 | //insert 8 | for(int i=1;i1) 33 | { 34 | int temp=arr[0]; 35 | arr[0]=arr[size2-1]; 36 | arr[size2-1]=temp; 37 | size2--; 38 | int PI=0; 39 | int LCI=2*PI+1; 40 | int RCI=2*PI+2; 41 | 42 | while(LCIarr[LCI]) 46 | { 47 | MI=LCI; 48 | } 49 | if(RCIarr[RCI]) 50 | { 51 | MI=RCI; 52 | } 53 | if(MI==PI) 54 | { 55 | break; 56 | } 57 | int temp=arr[MI]; 58 | arr[MI]=arr[PI]; 59 | arr[PI]=temp; 60 | 61 | PI=MI; 62 | LCI=2*PI+1; 63 | RCI=2*PI+2; 64 | 65 | } 66 | 67 | 68 | 69 | } 70 | 71 | 72 | } 73 | 74 | int main() 75 | { 76 | int arr[]={100,10,15,4,17,21,67}; 77 | int n=sizeof(arr)/sizeof(arr[0]); 78 | insertHeapusingArray(arr,n); 79 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector removeDuplicate(int *arr,int size) 7 | { 8 | vectoroutput; 9 | unordered_mapchecknum; 10 | for(int i=0;i0) 13 | { 14 | continue; 15 | } 16 | checknum[arr[i]]=true; 17 | output.push_back(arr[i]); 18 | } 19 | return output; 20 | 21 | } 22 | 23 | int main() 24 | { 25 | int arr[]={1,4,3,7,8,9,1,4,2,7,7,7}; 26 | int size=sizeof(arr)/sizeof(arr[0]); 27 | vector output=removeDuplicate(arr,size); 28 | for(int i=0;i 2 | using namespace std; 3 | 4 | /* 5 | You will be given an array containing only 0s, 1s and 2s. 6 | You have sort the array in linear time that is O(N) where N is the size of the array. 7 | 8 | */ 9 | 10 | void linear_time(int a[], int n){ 11 | int low=0, mid=0, high=n-1; 12 | while(mid<=high){ 13 | if(a[mid]==0){ 14 | swap(a[mid],a[low]); 15 | mid++; 16 | low++; 17 | } 18 | else if(a[mid]==1){ 19 | mid++; 20 | } 21 | else{ 22 | swap(a[mid],a[high]); 23 | high--; 24 | } 25 | } 26 | } 27 | 28 | int main(){ 29 | int n; 30 | cin>>n; 31 | int a[n]; 32 | for(int i=0;i>a[i]; 34 | } 35 | linear_time(a,n); 36 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector removeDuplicate(string arr) 7 | { 8 | vectoroutput; 9 | unordered_mapchecknum; 10 | for(int i=0;i0) 13 | { 14 | continue; 15 | } 16 | checknum[arr[i]]=true; 17 | output.push_back(arr[i]); 18 | } 19 | 20 | return output; 21 | } 22 | 23 | int main() 24 | { 25 | string arr="abcd"; 26 | // int length=arr.length(); 27 | vectoroutput= removeDuplicate(arr); 28 | for(int i=0;i 2 | using namespace std; 3 | #define endl '\n' 4 | #define ll long long 5 | #define mod 1000000007 6 | ll dp[1000001]; // dp[i] stores the hash value of substring starting from 0 to index i; 7 | ll inverse[1000001]; // ith index store inverse of the value p^i; 8 | ll findPower(ll a,ll n) 9 | { 10 | ll res=1; 11 | while(n>0) 12 | { 13 | if(n&1) res=(res*a)%mod; 14 | a=(a*a)%mod; 15 | n>>=1; 16 | } 17 | return res; 18 | } 19 | void init(string s) 20 | { 21 | ll p=31; 22 | ll power=1; 23 | ll value=0; 24 | dp[0]=(s[0]-'a'+1); 25 | value+=(s[0]-'a'+1); 26 | inverse[0]=1; 27 | for(int i=1;i0) res-=dp[l-1]; 39 | res=(res*inverse[l])%mod; 40 | return res; 41 | } 42 | void solve() 43 | { 44 | ll l,r; 45 | cin>>l>>r; 46 | cout<>s; 52 | init(s); 53 | int t; 54 | cin>>t; 55 | while(t--) 56 | { 57 | solve(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /towerofhanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void towerOfHanoi(int n, char from_rod, 5 | char to_rod, char aux_rod) 6 | { 7 | if (n == 1) 8 | { 9 | cout << "Move disk 1 from rod " << from_rod << 10 | " to rod " << to_rod< 2 | using namespace std; 3 | #define V 4 4 | 5 | int travllingSalesmanProblem(int graph[][V], int s) 6 | { 7 | vector vertex; 8 | for (int i = 0; i < V; i++) 9 | if (i != s) 10 | vertex.push_back(i); 11 | 12 | int min_path = INT_MAX; 13 | do { 14 | 15 | int current_pathweight = 0; 16 | 17 | int k = s; 18 | for (int i = 0; i < vertex.size(); i++) { 19 | current_pathweight += graph[k][vertex[i]]; 20 | k = vertex[i]; 21 | } 22 | current_pathweight += graph[k][s]; 23 | 24 | min_path = min(min_path, current_pathweight); 25 | 26 | } while (next_permutation(vertex.begin(), vertex.end())); 27 | 28 | return min_path; 29 | } 30 | 31 | int main() 32 | { 33 | int graph[][V] = { { 0, 10, 15, 20 }, 34 | { 10, 0, 35, 25 }, 35 | { 15, 35, 0, 30 }, 36 | { 20, 25, 30, 0 } }; 37 | int s = 0; 38 | cout << travllingSalesmanProblem(graph, s) << endl; 39 | return 0; 40 | } 41 | --------------------------------------------------------------------------------