├── Array ├── Element_with_left_side_smaller_and_right_side_greater.cpp ├── Kadane_Algorithm ├── Kth_largest_in_stream.cpp ├── Merge_Without_Extra_Space ├── Peak element ├── Two_stacks_in_an_array ├── Union_of_two_sorted_arrays ├── array_zig_zag.cpp ├── choclate_distribution_problem.cpp ├── equilibrium_point.cpp ├── find_element_that_appears_twice.cpp ├── kth_smallest.cpp ├── largest_number_formed_from_array.cpp ├── largest_sum_subarray.cpp ├── leaders.cpp ├── maximum_of_all_subarray_size_k.cpp ├── maximum_sum_increasing_subsequence.cpp ├── minimum_platforms.cpp ├── missing_number.cpp ├── pythagorean_triplet.cpp ├── relative_sorting.cpp ├── reverse_in_group.cpp ├── sort_012.cpp ├── sort_elements_by_frequency.cpp ├── spiral_matrix.cpp ├── stock_buy_sell.cpp ├── subarray_given_sum.cpp └── trapping_rain_water.cpp ├── Bit Magic ├── bit_difference.cpp ├── check_kth_bit.cpp ├── count_total_set_bits.cpp ├── first_set_bit.cpp ├── longest_consecutive_1's.cpp ├── number_is_sparse_or_not.cpp ├── power_of_2.cpp ├── rightmost_different_bit.cpp ├── set_kth_bit.cpp ├── swap_all_even_odd_bits.cpp └── toggle_bits_given_range.cpp ├── Divide and Conquer ├── binary_search_iterative.cpp ├── binary_search_recursive.cpp ├── kth_element_in_two_sorted_arrays.cpp ├── merge_sort.cpp └── quick_sort.cpp ├── Dynamic Programming ├── 0-1_knapsack.cpp ├── a.exe ├── binomial_coeff.cpp ├── box_stacking.cpp ├── coin_change.cpp ├── count_number_of_ways.cpp ├── cutted_segments.cpp ├── edit_distance.cpp ├── fibbonaci_series.cpp ├── longest_common_subsequence.cpp ├── longest_increasing_subsequence.cpp ├── max_size_square_submatrix_all1.cpp ├── maximum_weighted_path.cpp ├── minimum_jumps.cpp ├── number_of_paths.cpp ├── pattern.cpp ├── special_keyboard.cpp └── subset_sum_problem.cpp ├── Graph Algorithms ├── bfs.cpp ├── check_whether_path_exist.cpp ├── dfs.cpp ├── djikstra.cpp ├── flood_fill_algorithm.cpp ├── minimum_cost_path.cpp ├── number_of_islands.cpp └── shortest_path_to_destination.cpp ├── Greedy Algorithms ├── activity_selection.cpp ├── geek_collects_the_ball.cpp ├── largest_number_possible.cpp ├── maximize_toys.cpp ├── maximum_length_chain.cpp ├── minimize_dot_product.cpp ├── minimize_heights.cpp ├── minimum_number_of_coins.cpp ├── minimum_operations.cpp ├── minimum_spanning_tree.cpp ├── n_meetings_in_room.cpp ├── pagefaults_lru.cpp └── shop_in_candy_store.cpp ├── Hashing ├── array_pair_sum_divisibility.cpp ├── array_subset_of_other.cpp ├── check_if_frequency_can_be_equal.cpp ├── check_two_array_equal.cpp ├── count_distinct_in_every_window.cpp ├── find_all_pairs_with_given_sum.cpp ├── find_first_repeated_character.cpp ├── first_element_to_occur_k_times.cpp ├── largest_subarray_zero_sum.cpp ├── minimum_indexed_character.cpp ├── smallest_window_contains_all_characters.cpp ├── swapping_pair_make_equal_sum.cpp └── uncommon_characters.cpp ├── Interview Preparation Books ├── Cracking the Coding Interview.pdf ├── Gayle Laakmann McDowell-Cracking the Coding Interview, 189 Programming Questions and Solutions-CareerCup (2015).pdf └── Programming Interviews Exposed 3rd.pdf ├── Mathematical ├── gcd_lcm.cpp └── lcm_array.cpp ├── README.md ├── Searching └── linear_search.cpp ├── Smallest positive missing number ├── Sorting ├── bubble_sort.cpp ├── counting_sort.cpp ├── insertion_sort.cpp └── selection_sort.cpp ├── Stack-Queue ├── bracket_number.cpp ├── celebrity_problem.cpp ├── first_non_repeating_character.cpp ├── next_larger_element.cpp ├── number_following_pattern.cpp ├── rotten_oranges.cpp └── stock_span_problem.cpp ├── String ├── all_permutation.cpp ├── anagrams.cpp ├── implement_atoi.cpp ├── implement_strstr.cpp ├── longest_common_prefix.cpp ├── longest_common_substring.cpp ├── longest_palindromic_substring.cpp ├── min_insertions_to_form_palindrome.cpp ├── parenthesis_checker.cpp ├── recursively_remove_all_duplicates.cpp ├── remove_duplicates.cpp ├── reverse_words_in_string.cpp ├── roman_to_decimal.cpp └── string_rotated_by_two_places.cpp ├── input.txt ├── output.txt ├── template.cpp └── template.exe /Array/Element_with_left_side_smaller_and_right_side_greater.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int a[n+5]; 23 | for(int i=0;i> a[i]; 25 | 26 | int flag = 0; 27 | int left[n+5]; 28 | int right[n+5]; 29 | left[0] = a[0]; 30 | right[n-1] = a[n-1]; 31 | for(int i=1;i=0;i--) 36 | { 37 | right[i] = min(a[i],right[i+1]); 38 | } 39 | for(int i=1;i<=n-2;i++) 40 | { 41 | if(a[i] >= left[i] && a[i] <= right[i]) 42 | { 43 | cout << a[i] << endl; 44 | flag = 1; 45 | break; 46 | } 47 | } 48 | if(!flag) 49 | cout << -1 << endl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Array/Kadane_Algorithm: -------------------------------------------------------------------------------- 1 | // Function to find subarray with maximum sum 2 | // arr: input array 3 | // n: size of array 4 | int maxSubarraySum(int arr[], int n){ 5 | int sum=0; 6 | int maxi=INT_MIN; 7 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int k,n; 21 | cin >> k >> n; 22 | int a[n+5]; 23 | priority_queue pq; 24 | for(int i=0;i> a[i]; 27 | if(i < k-1) 28 | { 29 | pq.push(a[i]); 30 | cout << -1 << " " ; 31 | } 32 | else 33 | { 34 | pq.push(a[i]); 35 | int x = k-1; 36 | vector v; 37 | while(x--) 38 | { 39 | v.push_back(pq.top()); 40 | pq.pop(); 41 | } 42 | cout << pq.top() << " " ; 43 | for(int j=0;j 0; gap = nextGap(gap)) 14 | { 15 | for (i = 0; i + gap < n; i++) 16 | if (arr1[i] > arr1[i + gap]) 17 | swap(arr1[i], arr1[i + gap]); 18 | 19 | for (j = gap > n ? gap-n : 0 ; i < n&&j < m; i++, j++) 20 | if (arr1[i] > arr2[j]) 21 | swap(arr1[i], arr2[j]); 22 | 23 | if (j < m) 24 | { 25 | for (j = 0; j + gap < m; j++) 26 | if (arr2[j] > arr2[j + gap]) 27 | swap(arr2[j], arr2[j + gap]); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Array/Peak element: -------------------------------------------------------------------------------- 1 | Q. 2 | Given an array A of N integers. The task is to find a peak element in A in O( log N ) . 3 | An array element is peak if it is not smaller than its neighbours. For corner elements, consider only one neighbour. 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 3 9 | A[] = {1,2,3} 10 | Output: 2 11 | Explanation: In the given array, 12 | 3 is the peak element as it is 13 | greater than its neighbour. 14 | 15 | Answer 16 | #include 17 | using namespace std; 18 | 19 | // } Driver Code Ends 20 | 21 | int peakElement(int arr[], int n) 22 | { 23 | // Your code here 24 | int low=0,high=(n-1); 25 | while(low<=high){ 26 | int mid=(low+high)/2; 27 | if((mid>0 && mid<(n-1) && arr[mid]>=arr[mid-1] && arr[mid]>=arr[mid+1]) || (mid==0 && arr[mid]>=arr[mid+1]) || mid==(n-1) && arr[mid]>=arr[mid-1]){ 28 | return mid; 29 | } 30 | else if(arr[mid+1]>=arr[mid]){ 31 | low=(mid+1); 32 | } 33 | else if(arr[mid-1]>=arr[mid]){ 34 | high=(mid-1); 35 | } 36 | } 37 | } 38 | 39 | // { Driver Code Starts. 40 | 41 | int main() { 42 | int t; 43 | cin>>t; 44 | while(t--) 45 | { 46 | int n; 47 | cin>>n; 48 | int a[n]; 49 | for(int i=0;i>a[i]; 52 | } 53 | bool f=0; 54 | int A = peakElement(a,n); 55 | 56 | if(n==1) 57 | f=1; 58 | else 59 | if(A==0 and a[0]>=a[1]) 60 | f=1; 61 | else if(A==n-1 and a[n-1]>=a[n-2]) 62 | f=1; 63 | else if(a[A] >=a[A+1] and a[A]>= a[A-1]) 64 | f=1; 65 | else 66 | f=0; 67 | 68 | cout< 22 | 23 | using namespace std; 24 | 25 | class twoStacks 26 | { 27 | int *arr; 28 | int size; 29 | int top1, top2; 30 | public: 31 | twoStacks(int n=100){size = n; arr = new int[n]; top1 = -1; top2 = size;} 32 | 33 | void push1(int x); 34 | void push2(int x); 35 | int pop1(); 36 | int pop2(); 37 | }; 38 | 39 | 40 | 41 | int main() 42 | { 43 | 44 | int T; 45 | cin>>T; 46 | while(T--) 47 | { 48 | twoStacks *sq = new twoStacks(); 49 | 50 | int Q; 51 | cin>>Q; 52 | while(Q--){ 53 | int stack_no; 54 | cin>>stack_no; 55 | int QueryType=0; 56 | cin>>QueryType; 57 | 58 | if(QueryType==1) 59 | { 60 | int a; 61 | cin>>a; 62 | if(stack_no ==1) 63 | sq->push1(a); 64 | else if(stack_no==2) 65 | sq->push2(a); 66 | }else if(QueryType==2){ 67 | if(stack_no==1) 68 | cout<pop1()<<" "; 69 | else if(stack_no==2) 70 | cout<pop2()<<" "; 71 | 72 | } 73 | } 74 | cout< 17 | using namespace std; 18 | 19 | 20 | // } Driver Code Ends 21 | 22 | 23 | vector findUnion(int arr1[], int arr2[], int n, int m) 24 | { 25 | //Your code here 26 | //return vector with correct order of elements 27 | vector ans; 28 | int i=0,j=0; 29 | if(arr1[i]>arr2[j]){ 30 | ans.push_back(arr2[j]); 31 | j++; 32 | } 33 | else if(arr1[i]=0){ 44 | i++; 45 | continue; 46 | } 47 | if(arr2[j]==ans.back() && j>=0){ 48 | j++; 49 | continue; 50 | } 51 | if(arr1[i]arr2[j]){ 56 | ans.push_back(arr2[j]); 57 | j++; 58 | } 59 | else{ 60 | ans.push_back(arr1[i]); 61 | i++; 62 | j++; 63 | } 64 | } 65 | while(i> T; 86 | 87 | while(T--){ 88 | 89 | 90 | 91 | int N, M; 92 | cin >>N >> M; 93 | 94 | int arr1[N]; 95 | int arr2[M]; 96 | 97 | for(int i = 0;i> arr1[i]; 99 | } 100 | 101 | for(int i = 0;i> arr2[i]; 103 | } 104 | 105 | vector ans = findUnion(arr1,arr2, N, M); 106 | for(int i: ans)cout< 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | int n,i; 22 | cin >> n; 23 | int a[n]; 24 | for(int i=0;i> a[i]; 27 | } 28 | for(i=0;i a[i+1]) 33 | { 34 | int temp; 35 | temp = a[i]; 36 | a[i] = a[i+1]; 37 | a[i+1] = temp; 38 | 39 | } 40 | } 41 | else 42 | { 43 | if(a[i] < a[i+1]) 44 | { 45 | int temp; 46 | temp = a[i]; 47 | a[i] = a[i+1]; 48 | a[i+1] = temp; 49 | 50 | } 51 | } 52 | } 53 | for(i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,k; 21 | cin >> n; 22 | int a[n]; 23 | int mn = INT_MAX; 24 | for(int i=0;i> a[i]; 27 | } 28 | cin >> k; 29 | sort(a,a+n); 30 | for(int i=0;i 10 | using namespace std; 11 | #define LL long long 12 | #define F first 13 | #define S second 14 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 15 | int equilibrium_point(int a[],int n) 16 | { 17 | int sum =0,leftsum=0,rightsum; 18 | 19 | for(int i=0;i> n; 45 | int a[n+5]; 46 | for(int i=0;i> a[i]; 48 | cout << equilibrium_point(a,n) << endl; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Array/find_element_that_appears_twice.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int a[n+5]; 23 | int ans = 0; 24 | for(int i=0;i> a[i]; 26 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | fast_io; 17 | if(fopen("input.txt", "r")) 18 | { 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt","w",stdout); 21 | } 22 | int t; 23 | cin >> t; 24 | while(t--) 25 | { 26 | int n; 27 | cin >> n; 28 | int a[n+5]; 29 | priority_queue, greater> pq; 30 | for(int i=0;i> a[i]; 34 | pq.push(a[i]); 35 | } 36 | int k; 37 | cin >> k; 38 | k--; 39 | while(k--) 40 | { 41 | pq.pop(); 42 | } 43 | cout << pq.top() << endl; 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Array/largest_number_formed_from_array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int cmp(string a,string b) 15 | { 16 | string ab = a+b; 17 | string ba = b+a; 18 | 19 | if(ab > ba) 20 | return 1; 21 | else 22 | return 0; 23 | } 24 | int main() 25 | { 26 | int t; 27 | cin >> t; 28 | while(t--) 29 | { 30 | int n; 31 | cin >> n; 32 | string s[n+5]; 33 | for(int i=0;i> s[i]; 35 | sort(s,s+n,cmp); 36 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int largest_sum_subarray(int a[],int n) 15 | { 16 | int max_so_far = 0; 17 | int max_ending_here = 0; 18 | 19 | for(int i=0;i> n; 47 | int a[n+5]; 48 | for (int i=0;i> a[i]; 50 | 51 | cout << largest_sum_subarray(a,n) << endl; 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Array/leaders.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | 15 | int leaders(int a[],int n) 16 | { 17 | stack s; 18 | int mx = a[n-1]; 19 | for(int i=n-2;i>=0;i--) 20 | { 21 | if(a[i] > mx) 22 | { 23 | s.push(a[i]); 24 | mx = a[i]; 25 | } 26 | } 27 | while(!s.empty()) 28 | { 29 | cout << s.top() << " "; 30 | s.pop(); 31 | } 32 | cout << a[n-1] << " "; 33 | cout << endl; 34 | } 35 | int main() 36 | { 37 | fast_io; 38 | if(fopen("input.txt", "r")) 39 | { 40 | freopen("input.txt", "r", stdin); 41 | freopen("output.txt","w",stdout); 42 | } 43 | int t; 44 | cin >> t; 45 | while(t--) 46 | { 47 | int n; 48 | cin >> n; 49 | int a[n+5]; 50 | for(int i=0;i> a[i]; 52 | leaders(a,n); 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Array/maximum_of_all_subarray_size_k.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | fast_io; 17 | if(fopen("input.txt", "r")) 18 | { 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt","w",stdout); 21 | } 22 | int t; 23 | cin >> t; 24 | while(t--) 25 | { 26 | int n,k; 27 | cin >> n >> k; 28 | int a[n+5]; 29 | for(int i=0;i> a[i]; 31 | multiset s; 32 | for(int i=0;i :: iterator it; 38 | it = s.end(); 39 | it--; 40 | cout << *it << " "; 41 | for(int i=k;i :: iterator it1,it2,it; 45 | it1 = s.find(a[start]); 46 | it2 = it1; 47 | it2++; 48 | s.erase(it1,it2); 49 | it = s.end(); 50 | it--; 51 | cout << *it << " "; 52 | start++; 53 | } 54 | cout << endl; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Array/maximum_sum_increasing_subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int maximum_sum_increasing_subsequence(int a[],int n) 15 | { 16 | int mx = -1; 17 | int dp[n+1]; 18 | for(int i=0;i dp[i]) 27 | dp[i] = a[i]+dp[j]; 28 | } 29 | } 30 | for(int i=0;i> t; 45 | while(t--) 46 | { 47 | int n; 48 | cin >> n; 49 | int a[n+5]; 50 | for(int i=0;i> a[i]; 53 | } 54 | cout << maximum_sum_increasing_subsequence(a,n) << endl; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Array/minimum_platforms.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | fast_io; 17 | if(fopen("input.txt", "r")) 18 | { 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt","w",stdout); 21 | } 22 | int t; 23 | cin >> t; 24 | while(t--) 25 | { 26 | int n; 27 | cin >> n; 28 | int a[n],d[n]; 29 | for(int i=0;i> a[i]; 32 | } 33 | for(int i=0;i> d[i]; 36 | if(d[i] < a[i]) 37 | d[i] += 2400; 38 | } 39 | sort(a,a+n); 40 | sort(d,d+n); 41 | int ans = 1,i=1,j=0; 42 | int platfroms = 1; 43 | while(i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int missing_number(int a[],int n) 15 | { 16 | int sum=0; 17 | for(int i=0;i> n; 34 | int a[n+5]; 35 | for(int i=0;i> a[i]; 37 | 38 | cout << missing_number(a,n) << endl; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Array/pythagorean_triplet.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | bool isTriplet(int arr[], int n) 15 | { 16 | for (int i=0; i= 2; i--) 21 | { 22 | int l = 0; 23 | int r = i-1; 24 | while (l < r) 25 | { 26 | 27 | if (arr[l] + arr[r] == arr[i]) 28 | return true; 29 | (arr[l] + arr[r] < arr[i])? l++: r--; 30 | } 31 | } 32 | return false; 33 | } 34 | int main() 35 | { 36 | int t; 37 | cin >> t; 38 | while(t--) 39 | { 40 | int n; 41 | cin >> n; 42 | int arr[n]; 43 | for(int i=0;i> arr[i]; 46 | } 47 | isTriplet(arr, n)? cout << "Yes": cout << "No"; 48 | cout << endl; 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Array/relative_sorting.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int m,n; 21 | cin >> m >> n; 22 | int hash[10005]; 23 | memset(hash,0,sizeof(hash)); 24 | int a1[m+5],a2[n+5]; 25 | for(int i=0;i> a1[i]; 28 | hash[a1[i]]++; 29 | } 30 | for(int i=0;i> a2[i]; 33 | if(hash[a2[i]] > 0) 34 | { 35 | for(int j=1;j<=hash[a2[i]];j++) 36 | { 37 | cout << a2[i] << " "; 38 | } 39 | hash[a2[i]] = 0; 40 | } 41 | } 42 | for(int i=1;i<=10000;i++) 43 | { 44 | if(hash[i] > 0) 45 | { 46 | for(int j=1;j<=hash[i];j++) 47 | cout << i << " "; 48 | } 49 | } 50 | cout << endl; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Array/reverse_in_group.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | fast_io; 17 | if(fopen("input.txt", "r")) 18 | { 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt","w",stdout); 21 | } 22 | int t; 23 | cin >> t; 24 | while(t--) 25 | { 26 | int n; 27 | cin >> n; 28 | int a[n+5]; 29 | for(int i=0;i> a[i]; 32 | } 33 | int k; 34 | cin >> k; 35 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int sort_012(int a[],int n) 15 | { 16 | int l=0,m=0,h=n-1; 17 | 18 | while(m<=h) 19 | { 20 | switch(a[m]) 21 | { 22 | case 0: 23 | { 24 | int tmp; 25 | tmp = a[l]; 26 | a[l] = a[m]; 27 | a[m] = tmp; 28 | l++,m++; 29 | break; 30 | } 31 | case 1: 32 | m++; 33 | break; 34 | case 2: 35 | { 36 | int tmp; 37 | tmp = a[h]; 38 | a[h] = a[m]; 39 | a[m] = tmp; 40 | h--; 41 | break; 42 | } 43 | 44 | } 45 | } 46 | } 47 | int main() 48 | { 49 | fast_io; 50 | if(fopen("C:/Users/Jatin Goel/Desktop/Must-Do-Coding-Questions/input.txt", "r")) 51 | { 52 | freopen("C:/Users/Jatin Goel/Desktop/Must-Do-Coding-Questions/input.txt", "r", stdin); 53 | freopen("C:/Users/Jatin Goel/Desktop/Must-Do-Coding-Questions/output.txt","w",stdout); 54 | } 55 | int n; 56 | cin >> n; 57 | int a[n+5]; 58 | 59 | for(int i=0;i> a[i]; 61 | sort_012(a,n); 62 | 63 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int a[n+5]; 23 | vector< pair > v; 24 | int hash[10005]={0}; 25 | for(int i=0;i> a[i]; 28 | } 29 | stable_sort(a,a+n); 30 | for(int i=0;i 0) 36 | v.push_back(make_pair(hash[i],mx-i)); 37 | } 38 | stable_sort(v.rbegin(),v.rend()); 39 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int m = 4,n= 4; 21 | int a[10][10]; 22 | for(int i=0;i> a[i][j]; 25 | 26 | while (k < m && l < n) 27 | { 28 | for (i = l; i < n; ++i) 29 | { 30 | cout << a[k][i] << " "; 31 | } 32 | k++ 33 | for (i = k; i < m; ++i) 34 | { 35 | cout << a[i][n-1] << " "; 36 | } 37 | n--; 38 | if ( k < m) 39 | { 40 | for (i = n-1; i >= l; --i) 41 | { 42 | cout << a[m-1][i] << " "; 43 | } 44 | m--; 45 | } 46 | if (l < n) 47 | { 48 | for (i = m-1; i >= k; --i) 49 | { 50 | cout << a[i][l] << " "; 51 | } 52 | l++; 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Array/stock_buy_sell.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int price[n+5]; 23 | for(int i=0;i> price[i]; 25 | 26 | int buy = -1,sell =-1; 27 | int flag = 0; 28 | int i=0; 29 | while(i < n-1) 30 | { 31 | while(i < n-1 && price[i] >= price[i+1]) 32 | i++; 33 | if(i == n-1) 34 | break; 35 | buy = i++; 36 | 37 | while(i < n && price[i] >= price[i-1]) 38 | i++; 39 | sell = i-1; 40 | flag = 1; 41 | cout << "(" << buy << " " << sell << ")" << " "; 42 | 43 | } 44 | if(!flag) 45 | cout << "No Profit" << endl; 46 | else 47 | cout << endl; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Array/subarray_given_sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int subarray_given_sum(int a[],int n,int x) 15 | { 16 | int sum = a[0]; 17 | int start = 0,end = -1,found=0; 18 | for(int i=1;i<=n;i++) 19 | { 20 | while(sum > x && start < i-1) 21 | sum -= a[start++]; 22 | 23 | if(sum == x) 24 | { 25 | end = i; 26 | found = 1; 27 | break; 28 | } 29 | if(i < n ) 30 | sum = sum + a[i]; 31 | 32 | } 33 | if(found) 34 | { 35 | cout << "start:"<> n >> x; 53 | int a[n+5]; 54 | for(int i=0;i> a[i]; 57 | } 58 | subarray_given_sum(a,n,x); 59 | } 60 | -------------------------------------------------------------------------------- /Array/trapping_rain_water.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | fast_io; 17 | if(fopen("input.txt", "r")) 18 | { 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt","w",stdout); 21 | } 22 | int t; 23 | cin >> t; 24 | while(t--) 25 | { 26 | int n; 27 | cin >> n; 28 | int a[n+5]; 29 | for(int i=0;i> a[i]; 31 | int l[n+5],r[n+5]; 32 | l[0] = a[0]; 33 | for(int i=1;i=0;i--) 38 | r[i] = max(a[i],r[i+1]); 39 | 40 | int ans = 0; 41 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int a,b; 21 | cin >> a >> b; 22 | int ctr=0; 23 | while(a > 0 || b > 0) 24 | { 25 | if((a%2)^(b%2)) 26 | ctr++; 27 | a/=2; 28 | b/=2; 29 | } 30 | cout << ctr << endl; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Bit Magic/check_kth_bit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | LL n,k; 21 | cin >> n >> k; 22 | int ctr=0; 23 | string ans = "No"; 24 | while(n>0) 25 | { 26 | if(ctr == k) 27 | { 28 | if(n%2) 29 | ans = "Yes"; 30 | break; 31 | } 32 | n = n/2; 33 | ctr++; 34 | } 35 | cout << ans << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Bit Magic/count_total_set_bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int ans = 0; 23 | for(int i=1;i<=n;i++) 24 | { 25 | int k = i; 26 | while(k) 27 | { 28 | if(k%2) 29 | ans++; 30 | k/=2; 31 | } 32 | } 33 | cout << ans << endl; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Bit Magic/first_set_bit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int flag = 1,ctr =0; 23 | while(n > 0 && flag) 24 | { 25 | if(n%2 == 1) 26 | flag = 0; 27 | n = n/2; 28 | ctr++; 29 | } 30 | cout << ctr << endl; 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Bit Magic/longest_consecutive_1's.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | 19 | while(t--) 20 | { 21 | int n; 22 | cin >> n; 23 | int ctr=0,ans=0; 24 | while(n) 25 | { 26 | if(n%2) 27 | ctr++; 28 | else 29 | ctr = 0; 30 | ans = max(ctr,ans); 31 | n/=2; 32 | } 33 | cout << ans << endl; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Bit Magic/number_is_sparse_or_not.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int ans = 1; 23 | int ctr=0; 24 | while(n>0) 25 | { 26 | if(n%2) 27 | ctr++; 28 | else 29 | ctr = 0; 30 | if(ctr >= 2) 31 | { 32 | ans = 0; 33 | break; 34 | } 35 | n/=2; 36 | } 37 | cout << ans < 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | LL n; 21 | cin >> n; 22 | if(n==0) 23 | { 24 | cout << "NO"< 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int m,n; 21 | int ctr=0; 22 | cin >> m >> n; 23 | while(m > 0 || n > 0) 24 | { 25 | ctr++; 26 | if((m%2)^(n%2)) 27 | { 28 | break; 29 | } 30 | m/=2; 31 | n/=2; 32 | } 33 | cout << ctr << endl; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Bit Magic/set_kth_bit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,k; 21 | cin >> n >> k; 22 | string s = ""; 23 | while(n>0) 24 | { 25 | if(n%2) 26 | s = s + '1'; 27 | else 28 | s = s + '0'; 29 | n/=2; 30 | } 31 | 32 | s[k] = '1'; 33 | LL ans = 0; 34 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s(16,'0'); 21 | 22 | int i = 15; 23 | int n; 24 | while(n) 25 | { 26 | if(n%2) 27 | s[i--] = '1'; 28 | else 29 | s[i--] = '0'; 30 | n/=2; 31 | } 32 | for(int i=0;i=0;i--) 40 | { 41 | ans += (s[i] - '0')*pow(2,15-i); 42 | } 43 | cout << ans << endl; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Bit Magic/toggle_bits_given_range.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int l,r; 23 | cin >> l >> r; 24 | string s = ""; 25 | while(n>0) 26 | { 27 | if(n%2) 28 | s = s + '1'; 29 | else 30 | s = s + '0'; 31 | n/=2; 32 | } 33 | for(int i=0;i= l && i+1 <= r) 35 | s[i] = s[i]=='1'?'0':'1'; 36 | LL ans = 0; 37 | for(int i=0;i 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int binary_search_iterative(int a[],int l,int h,int x) 13 | { 14 | while(l<=h) 15 | { 16 | int mid = (l+h)/2; 17 | if(a[mid] == x) 18 | return mid; 19 | else if(a[mid] > x) 20 | h = mid-1; 21 | else 22 | l = mid+1; 23 | } 24 | return -1; 25 | } 26 | int main() 27 | { 28 | fast_io; 29 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 30 | { 31 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 32 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 33 | } 34 | int n,x; 35 | cin >> n; 36 | int a[n+5]; 37 | for(int i=0;i> a[i]; 39 | cin >> x; 40 | int idx = binary_search_iterative(a,0,n-1,x); 41 | if(idx==-1) 42 | cout << "Element not found!"< 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int binary_search_recursive(int a[],int l,int h,int x) 13 | { 14 | if(l<=h) 15 | { 16 | int mid = (l+h)/2; 17 | if(a[mid] == x) 18 | return mid; 19 | else if(a[mid] > x) 20 | return binary_search_recursive(a,l,mid-1,x); 21 | else 22 | return binary_search_recursive(a,mid+1,h,x); 23 | } 24 | return -1; 25 | } 26 | int main() 27 | { 28 | fast_io; 29 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 30 | { 31 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 32 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 33 | } 34 | int n,x; 35 | cin >> n; 36 | int a[n+5]; 37 | for(int i=0;i> a[i]; 39 | cin >> x; 40 | int idx = binary_search_recursive(a,0,n-1,x); 41 | if(idx==-1) 42 | cout << "Element not found!"< 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,m,k; 21 | cin >> n >> m >> k; 22 | int a[n+5],b[m+5]; 23 | for(int i=0;i> a[i]; 25 | for(int i=0;i> b[i]; 27 | int i=0,j=0,idx=0,ans; 28 | while(i 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int merge(int a[],int l,int m,int h) 13 | { 14 | int i,j,k,n1,n2; 15 | n1 = m-l+1; 16 | n2 = h-m; 17 | int L[n1],R[n2]; 18 | for(int i=0;i> n; 59 | int a[n+5]; 60 | for(int i=0;i> a[i]; 62 | merge_sort(a,0,n-1); 63 | for(int i=0;i 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int partition(int a[],int l,int h) 13 | { 14 | int pivot = a[h]; 15 | int i=l-1; 16 | for(int j=l;j> n; 53 | int a[n+5]; 54 | for(int i=0;i> a[i]; 56 | quick_sort(a,0,n-1); 57 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,W; 21 | cin >> n; 22 | cin >> W; 23 | int wt[n+5]; 24 | int val[n+5]; 25 | for(int i=1;i<=n;i++) 26 | { 27 | cin >> val[i]; 28 | } 29 | for(int i=1;i<=n;i++) 30 | { 31 | cin >> wt[i]; 32 | } 33 | int K[n+1][W+1]; 34 | for (int i = 0; i <= n; i++) 35 | { 36 | for (int w = 0; w <= W; w++) 37 | { 38 | if (i==0 || w==0) 39 | K[i][w] = 0; 40 | else if (wt[i] <= w) 41 | K[i][w] = max(val[i] + K[i-1][w-wt[i]], K[i-1][w]); 42 | else 43 | K[i][w] = K[i-1][w]; 44 | } 45 | } 46 | cout << K[n][W] << endl; 47 | 48 | } 49 | } -------------------------------------------------------------------------------- /Dynamic Programming/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/de-cryptor/Must-Do-Coding-Questions/857a96fe20ae6c51a517b105988416362d10aadd/Dynamic Programming/a.exe -------------------------------------------------------------------------------- /Dynamic Programming/binomial_coeff.cpp: -------------------------------------------------------------------------------- 1 | /* Jatin Goel 2 | IIIT Allahabad*/ 3 | /* There is no substitute of hardwork. */ 4 | /* Hardwork definitely pays off. */ 5 | /* There is no shortcut to success. */ 6 | #include 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int binomial_coeff(int n,int k) 13 | { 14 | int dp[n+5][k+5]; 15 | for(int i=0;i<=n;i++) 16 | { 17 | for(int j=0;j<=min(i,k);j++) 18 | { 19 | if(j == 0 || i == j) 20 | dp[i][j] = 1; 21 | else 22 | dp[i][j] = dp[i-1][j] + dp[i-1][j-1]; 23 | } 24 | } 25 | return dp[n][k]; 26 | } 27 | int main() 28 | { 29 | fast_io; 30 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 31 | { 32 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 33 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 34 | } 35 | int n,k; 36 | cin >> n >> k; 37 | cout << binomial_coeff(n,k) << endl; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Dynamic Programming/box_stacking.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | bool cmp(vector a,vector b) 15 | { 16 | return a[1]*a[2] < b[1]*b[2] ; 17 | } 18 | int maxHeight(int height[],int width[],int length[],int n) 19 | { 20 | vector < vector > boxes; 21 | for(int i=0;i box1,box2,box3; 24 | int h,w,l; 25 | h = height[i]; 26 | w = width[i]; 27 | l = length[i]; 28 | //box1 29 | box1.push_back(h); 30 | box1.push_back(min(w,l)); 31 | box1.push_back(max(w,l)); 32 | boxes.push_back(box1); 33 | //box2 34 | box2.push_back(w); 35 | box2.push_back(min(h,l)); 36 | box2.push_back(max(h,l)); 37 | boxes.push_back(box2); 38 | //box3 39 | box3.push_back(l); 40 | box3.push_back(min(h,w)); 41 | box3.push_back(max(h,w)); 42 | boxes.push_back(box3); 43 | 44 | } 45 | sort(boxes.rbegin(),boxes.rend(),cmp); 46 | n = boxes.size(); 47 | int dp[n+5]; 48 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int a[n]; 23 | for(int i=0;i> a[i]; 26 | } 27 | int m; 28 | cin >> m; 29 | int table[m+1]; 30 | memset(table,0,sizeof(table)); 31 | table[0] = 1; 32 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int dp[n]; 23 | dp[0] = 1; 24 | dp[1] = 2; 25 | dp[2] = 4; 26 | for(int i=3;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | int x,y,z; 22 | cin>>n; 23 | cin>>x>>y>>z; 24 | int dp[n+1]; 25 | dp[0]=0; 26 | for(int i=1;i<=n;i++){ 27 | dp[i]=INT_MIN; 28 | } 29 | for(int i=1;i<=n;i++){ 30 | if(x<=i) 31 | dp[i]=max(dp[i],1+dp[i-x]); 32 | if(y<=i) 33 | dp[i]=max(dp[i],1+dp[i-y]); 34 | if(z<=i) 35 | dp[i]=max(dp[i],1+dp[i-z]); 36 | } 37 | cout< 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,m; 21 | cin >> m >> n; 22 | string s1,s2; 23 | cin >> s1 >> s2; 24 | int dp[m+1][n+1]; 25 | for(int i=0;i<=m;i++) 26 | { 27 | for(int j=0;j<=n;j++) 28 | { 29 | if(i==0) 30 | dp[i][j] = j; 31 | else if (j==0) 32 | dp[i][j] = i; 33 | else if(s1[i-1] == s2[j-1]) 34 | dp[i][j] = dp[i-1][j-1]; 35 | else 36 | dp[i][j] = 1+min(dp[i][j-1],min(dp[i-1][j],dp[i-1][j-1])); 37 | } 38 | } 39 | cout << dp[m][n] << endl; 40 | } 41 | } -------------------------------------------------------------------------------- /Dynamic Programming/fibbonaci_series.cpp: -------------------------------------------------------------------------------- 1 | /* Jatin Goel 2 | IIIT Allahabad*/ 3 | /* There is no substitute of hardwork. */ 4 | /* Hardwork definitely pays off. */ 5 | /* There is no shortcut to success. */ 6 | #include 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int main() 13 | { 14 | fast_io; 15 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 16 | { 17 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 18 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 19 | } 20 | int n; 21 | cin >> n; 22 | LL fib[n+5]; 23 | 24 | fib[0] = 0; 25 | fib[1] = 1; 26 | for(int i=2;i<=n;i++) 27 | fib[i] = fib[i-1] + fib[i-2]; 28 | 29 | for(int i=0;i<=n;i++) 30 | cout << fib[i] << " "; 31 | cout << endl; 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Dynamic Programming/longest_common_subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int LCS(string s1,string s2) 15 | { 16 | int n1 = s1.size(), n2 = s2.size(); 17 | int dp[n1+1][n2+1]; 18 | for (int i=0;i<=n1;i++) 19 | { 20 | for(int j=0;j<=n2;j++) 21 | { 22 | if(i == 0 || j == 0) 23 | { 24 | dp[i][j] = 0; 25 | } 26 | else if(s1[i-1] == s2[j-1]) 27 | { 28 | dp[i][j] = dp[i-1][j-1] + 1; 29 | } 30 | else 31 | { 32 | dp[i][j] = max(dp[i][j-1],dp[i-1][j]); 33 | } 34 | } 35 | } 36 | return dp[n1][n2]; 37 | } 38 | int main() 39 | { 40 | fast_io; 41 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 42 | { 43 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 44 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 45 | } 46 | string s1,s2; 47 | cin >> s1 >> s2; 48 | cout << LCS(s1,s2) << endl; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Dynamic Programming/longest_increasing_subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int a[n]; 23 | for(int i=0;i> a[i]; 26 | } 27 | int lis[n]; 28 | //memset(lis,1,sizeof(lis)); 29 | for(int i=0;i a[j] && lis[i] < 1+lis[j]) 38 | lis[i] = 1+lis[j]; 39 | } 40 | } 41 | int mx=0; 42 | for(int i=0;i mx) 45 | { 46 | mx=lis[i]; 47 | } 48 | } 49 | cout << mx << endl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Dynamic Programming/max_size_square_submatrix_all1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int M[100][100]; 15 | int max_size_submatrix(int m,int n) 16 | { 17 | int dp[n][m],max_size=0; 18 | for(int i=0;i> n >> m; 56 | for(int i=0;i> M[i][j]; 61 | } 62 | } 63 | cout << max_size_submatrix(m,n)< 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int m,n; 21 | cin >>n; 22 | m = n; 23 | int path[m+5][n+5]; 24 | int dp[m+5][n+5]; 25 | 26 | for(int i=0;i> path[i][j]; 31 | dp[i][j] = 0; 32 | } 33 | } 34 | for(int i=0;i=0) 42 | dp[i][j] = path[i][j] + max(dp[i-1][j],dp[i-1][j-1]); 43 | if(j+1 < n) 44 | dp[i][j] = max(dp[i][j],(path[i][j] + max(dp[i-1][j],dp[i-1][j+1]))); 45 | } 46 | } 47 | int ans =0; 48 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | #include 15 | using namespace std; 16 | int main() 17 | { 18 | int t; 19 | cin >> t; 20 | while(t--) 21 | { 22 | int n; 23 | cin >> n; 24 | int a[n]; 25 | for(int i=0;i> a[i]; 28 | } 29 | int jumps[n]; 30 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define PII pair 14 | 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | int n,m; 22 | cin >> n >> m; 23 | int dp[n+5][m+5]; 24 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 6 | { 7 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 8 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 9 | } 10 | int n; 11 | cin >> n; 12 | 13 | char m[1000][1000]; 14 | for(int i=0;i<1000;i++) 15 | for(int j=0;j<1000;j++) 16 | m[i][j] = ' '; 17 | int k = n; 18 | for(int i=0;i<1000;i++) 19 | m[k][i] = '*'; 20 | int ctr = 1,j=0,col=0; 21 | for(int ctr=1;ctr<=n;ctr++) 22 | { 23 | col += ctr+1; 24 | for(int k = n-ctr;k<=n+ctr;k++) 25 | m[k][col] = '*'; 26 | j = j+ctr; 27 | } 28 | int r = 2*n + 1; 29 | int c = (n*(n+1))/2 + n; 30 | 31 | for(int i=0;i<=r;i++) 32 | { 33 | for(int j=1;j<=c;j++) 34 | cout << m[i][j] << " "; 35 | cout << endl; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Dynamic Programming/special_keyboard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define PII pair 14 | 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | int N; 22 | cin >> N; 23 | int dp[N+5]; 24 | for(int i=1;i<=min(N,6);i++) 25 | dp[i] = i; 26 | 27 | for(int i=7;i<=N;i++) 28 | { 29 | int ans = 0; 30 | for(int j=i-3;j>=1;j--) 31 | { 32 | int curr = (i-j-1)*dp[j]; 33 | ans = max(ans,curr); 34 | } 35 | dp[i] = ans; 36 | } 37 | if (N > 75) 38 | cout << -1 << endl; 39 | else 40 | cout << dp[N] << endl; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Dynamic Programming/subset_sum_problem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | int subset_sum(int a[],int n,int sum) 15 | { 16 | bool dp[n+1][sum+1]; 17 | memset(dp,false,sizeof(dp)); 18 | for(int i=0;i<=n;i++) 19 | { 20 | dp[i][0] = true; 21 | } 22 | for(int i=1;i<=sum;i++) 23 | { 24 | dp[0][i] = false; 25 | } 26 | for(int i=1;i<=n;i++) 27 | { 28 | for(int j=1;j<=sum;j++) 29 | { 30 | if(j < a[i-1]) 31 | dp[i][j] = dp[i-1][j]; 32 | if(j >= a[i-1]) 33 | dp[i][j] = dp[i-1][j] | dp[i-1][j - a[i-1]]; 34 | } 35 | } 36 | return dp[n][sum]; 37 | } 38 | int main() 39 | { 40 | int t; 41 | cin >> t; 42 | while(t--) 43 | { 44 | int n; 45 | cin >> n; 46 | int a[n+5]; 47 | int sum = 0; 48 | for(int i=0;i> a[i]; 51 | sum += a[i]; 52 | } 53 | if(sum%2) 54 | cout << "NO" << endl; 55 | else 56 | { 57 | if(subset_sum(a,n,sum/2)) 58 | cout << "YES" << endl; 59 | else 60 | cout << "NO" << endl; 61 | } 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Graph Algorithms/bfs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | vector graph[1005]; 15 | int visited[1005]={0}; 16 | int main() 17 | { 18 | int V,E; 19 | cin >> V >> E; 20 | for(int i=0;i> x >> y; 24 | graph[x].push_back(y); 25 | graph[y].push_back(x); 26 | } 27 | int source; 28 | cin >> source; 29 | queue q; 30 | q.push(source); 31 | while(!q.empty()) 32 | { 33 | int x = q.front(); 34 | cout << x << " "; 35 | visited[x] = 1; 36 | q.pop(); 37 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | #define PII pair 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | int n; 22 | cin >> n; 23 | int graph[n+5][n+5]; 24 | PII source,goal; 25 | for(int i=0;i> graph[i][j]; 30 | if(graph[i][j] == 1) 31 | source = make_pair(i,j); 32 | if(graph[i][j] == 2) 33 | goal = make_pair(i,j); 34 | } 35 | } 36 | int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; 37 | queue< PII > q; 38 | q.push(source); 39 | int visited[n+5][n+5]; 40 | memset(visited,0,sizeof(visited)); 41 | int flag = 0; 42 | while(!q.empty()) 43 | { 44 | PII node = q.front(); 45 | q.pop(); 46 | visited[node.F][node.S] = 1; 47 | for(int i=0;i<4;i++) 48 | { 49 | int x = node.F + dir[i][0]; 50 | int y = node.S + dir[i][1]; 51 | if(graph[x][y] == 2) 52 | { 53 | flag = 1; 54 | break; 55 | } 56 | if(x >= 0 && x < n && y >= 0 && y < n) 57 | { 58 | if(graph[x][y] == 3 && !visited[x][y]) 59 | { 60 | q.push(make_pair(x,y)); 61 | } 62 | } 63 | } 64 | if(flag) 65 | break; 66 | 67 | } 68 | cout << flag << endl; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Graph Algorithms/dfs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | vector graph[1005]; 15 | int visited[1005]={0}; 16 | void dfs(int x) 17 | { 18 | visited[x] = 1; 19 | cout << x << " "; 20 | for(int i=0;i> V >> E; 33 | for(int i=0;i> x >> y; 37 | graph[x].push_back(y); 38 | graph[y].push_back(x); 39 | } 40 | int source; 41 | cin >> source; 42 | dfs(source); 43 | cout << endl; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Graph Algorithms/djikstra.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | #define PII pair 15 | void dijkstra(int graph[MAX][MAX], int s,int V) 16 | { 17 | int INF = 1e6; 18 | priority_queue < PII ,vector , greater > pq; 19 | vector dist(V, INF); 20 | pq.push(make_pair(0,s)); 21 | dist[s] = 0; 22 | while(!pq.empty()) 23 | { 24 | pair node = pq.top(); 25 | int u = node.second; 26 | pq.pop(); 27 | 28 | for(int i=0;i 0) 31 | { 32 | int v = i; 33 | int w = graph[u][i]; 34 | if(dist[v] > dist[u] + w) 35 | { 36 | dist[v] = dist[u] + w; 37 | pq.push(make_pair(dist[v],v)); 38 | } 39 | } 40 | } 41 | } 42 | 43 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define PII pair 14 | 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | int m,n; 22 | cin >> m >> n; 23 | int graph[m+5][n+5]; 24 | for(int i=0;i> graph[i][j]; 27 | int p,q,k; 28 | cin >> p >> q >> k; 29 | queue qu; 30 | qu.push(make_pair(p,q)); 31 | int color = graph[p][q]; 32 | graph[p][q] = k; 33 | while(!qu.empty()) 34 | { 35 | PII node = qu.front(); 36 | qu.pop(); 37 | int dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}}; 38 | for(int i=0;i<4;i++) 39 | { 40 | int x = dir[i][0] + node.F; 41 | int y = dir[i][1] + node.S; 42 | PII newnode = make_pair(x,y); 43 | if(x >= 0 && x < m && y >=0 && y 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define PII pair 14 | 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | int m,n; 22 | cin >> m ; 23 | n = m; 24 | int graph[m+5][n+5]; 25 | for(int i=0;i> graph[i][j]; 28 | int visited[m+5][n+5]; 29 | map< PII ,int> mp; 30 | memset(visited,0,sizeof(visited)); 31 | int dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}}; 32 | priority_queue < pair < int , PII > , vector < pair < int , PII > > , greater < pair < int , PII > > >pq; 33 | pq.push(make_pair(graph[0][0],make_pair(0,0))); 34 | visited[0][0] = 1; 35 | mp[make_pair(0,0)] = graph[0][0]; 36 | int ans = -1; 37 | while(!pq.empty()) 38 | { 39 | pair < int , PII > temp; 40 | temp = pq.top(); 41 | pq.pop(); 42 | PII node = temp.S; 43 | int dist = temp.F; 44 | for(int i=0;i<4;i++) 45 | { 46 | int x = dir[i][0] + node.F; 47 | int y = dir[i][1] + node.S; 48 | PII newnode = make_pair(x,y); 49 | if(x >= 0 && x < m && y >=0 && y 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | void dfs(int A[MAX][MAX], int N, int M,int visited[MAX][MAX],int p,int q) 15 | { 16 | visited[p][q] = 1; 17 | int dir[8][2] = { {1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1} }; 18 | for(int i=0;i<8;i++) 19 | { 20 | int x = p + dir[i][0]; 21 | int y = q + dir[i][1]; 22 | if(x >= 0 && x < N && y >= 0 && y < M) 23 | { 24 | if(A[x][y] & !visited[x][y]) 25 | { 26 | dfs(A,N,M,visited,x,y); 27 | } 28 | } 29 | } 30 | } 31 | int findIslands(int A[MAX][MAX], int N, int M) 32 | { 33 | int visited[MAX][MAX]; 34 | int islands = 0 ; 35 | memset(visited,0,sizeof(visited)); 36 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define PII pair 14 | 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | int m,n; 22 | cin >> m >> n; 23 | int graph[m+5][n+5]; 24 | for(int i=0;i> graph[i][j]; 27 | int p,q; 28 | cin >> p >> q; 29 | if(graph[0][0] == 0) 30 | { 31 | cout << -1 << endl; 32 | continue; 33 | } 34 | int visited[m+5][n+5]; 35 | map< PII ,int> mp; 36 | memset(visited,0,sizeof(visited)); 37 | int dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}}; 38 | priority_queue < pair < int , PII > , vector < pair < int , PII > > , greater < pair < int , PII > > >pq; 39 | pq.push(make_pair(0,make_pair(0,0))); 40 | visited[0][0] = 1; 41 | mp[make_pair(0,0)] = 0; 42 | int ans = -1; 43 | while(!pq.empty()) 44 | { 45 | pair < int , PII > temp; 46 | temp = pq.top(); 47 | pq.pop(); 48 | PII node = temp.S; 49 | int dist = temp.F; 50 | for(int i=0;i<4;i++) 51 | { 52 | int x = dir[i][0] + node.F; 53 | int y = dir[i][1] + node.S; 54 | PII newnode = make_pair(x,y); 55 | if(x >= 0 && x < m && y >=0 && y 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | pair p[10005]; 23 | for(int i=0;i> p[i].S; 25 | for(int i=0;i> p[i].F; 27 | sort(p,p+n); 28 | int ans = 1; 29 | int last_finish = p[0].F; 30 | for(int i=1;i= last_finish) 33 | { 34 | last_finish = p[i].F; 35 | ans++; 36 | } 37 | } 38 | cout << ans << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Greedy Algorithms/geek_collects_the_ball.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,m; 21 | cin >> m >> n; 22 | int a[m+5],b[n+5]; 23 | 24 | for(int i=0;i> a[i]; 26 | 27 | for(int i=0;i> b[i]; 29 | 30 | LL ans=0,sum1=0,sum2=0; 31 | int j=0,k=0; 32 | while(jb[k]) 38 | sum2 += b[k++]; 39 | 40 | else 41 | { 42 | sum1 += a[j++]; 43 | sum2 += b[k++]; 44 | ans += max(sum1,sum2); 45 | sum1=0; 46 | sum2=0; 47 | } 48 | } 49 | while(j 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,s; 21 | cin >> n >> s; 22 | 23 | if(n*9 < s || s == 0) 24 | cout << -1 << endl; 25 | else 26 | { 27 | string num = ""; 28 | for(int i=0;i=0;j--) 31 | { 32 | if(s >= j) 33 | { 34 | char c = ('0' + j); 35 | num = num + c; 36 | s -= j; 37 | break; 38 | } 39 | } 40 | } 41 | cout << num << endl; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Greedy Algorithms/maximize_toys.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int test; 17 | cin >> test; 18 | while(test--) 19 | { 20 | int n,amount; 21 | cin >> n >> amount; 22 | int t[n]; 23 | for(int i=0;i> t[i]; 25 | 26 | sort(t,t+n); 27 | int curr = 0,ctr=0; 28 | for(int i=0;i last_finish) 25 | { 26 | last_finish = p[i].second; 27 | ans++; 28 | } 29 | } 30 | return ans; 31 | } -------------------------------------------------------------------------------- /Greedy Algorithms/minimize_dot_product.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int a[n+5]; 23 | int b[n+5]; 24 | 25 | for(int i=0;i> a[i]; 27 | for(int i=0;i> b[i]; 29 | 30 | sort(a,a+n); 31 | sort(b,b+n); 32 | reverse(b,b+n); 33 | LL sum = 0; 34 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,k; 21 | cin >> k; 22 | cin >> n; 23 | int mn=1e6,mx = -1; 24 | int h[n+5]; 25 | for(int i=0;i> h[i]; 28 | mx = max(mx,h[i]); 29 | mn = min(mn,h[i]); 30 | } 31 | if(mx - mn <= k) 32 | { 33 | cout << mx - mn << endl; 34 | continue; 35 | } 36 | for(int i=0;i= mn+k) 47 | h[i] -= k; 48 | else 49 | { 50 | int p = mn + k; 51 | int q = mx - k; 52 | int diff1 = h[i] + k - q; 53 | int diff2 = p - (h[i] - k); 54 | 55 | if(diff1 <= diff2) 56 | { 57 | h[i] += k; 58 | } 59 | else 60 | { 61 | h[i] -= k; 62 | } 63 | } 64 | } 65 | 66 | } 67 | mn=1e6,mx = -1; 68 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int change[] = {1, 2, 5, 10, 20, 50, 100, 500, 1000}; 23 | int ans[10]={0}; 24 | 25 | for(int i=8;i>=0;i--) 26 | { 27 | if(n >= change[i]) 28 | { 29 | int d = n/change[i]; 30 | n -= d*change[i]; 31 | ans[i] = d; 32 | } 33 | } 34 | for(int i=8;i>=0;i--) 35 | { 36 | if(ans[i]) 37 | { 38 | for(int j=1;j<=ans[i];j++) 39 | { 40 | cout << change[i] << " "; 41 | } 42 | } 43 | } 44 | cout << endl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Greedy Algorithms/minimum_operations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int ctr=0; 23 | while(n) 24 | { 25 | ctr++; 26 | if(n%2) 27 | n -= 1; 28 | else 29 | n /= 2; 30 | } 31 | cout << ctr << endl; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Greedy Algorithms/minimum_spanning_tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define PII pair 14 | 15 | int spanningTree(vector > g[], int MAX) 16 | { 17 | priority_queue, greater > Q; 18 | int min_cost = 0; 19 | int source = 1; 20 | int visited[MAX]; 21 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | pair p[10005]; 23 | 24 | for(int i=0;i> p[i].S; 26 | 27 | for(int i=0;i> p[i].F; 29 | 30 | vector < pair < pair , int > > v; 31 | 32 | for(int i=0;i= last_finish) 44 | { 45 | last_finish = v[i].F.F; 46 | cout << v[i].S << " "; 47 | } 48 | } 49 | cout << endl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Greedy Algorithms/pagefaults_lru.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n; 21 | cin >> n; 22 | int pages[n+5]; 23 | for(int i=0;i> pages[i]; 25 | int capacity; 26 | cin >> capacity; 27 | 28 | unordered_set s; 29 | unordered_map indexes; 30 | 31 | int page_faults = 0; 32 | for (int i=0; i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,k; 21 | cin >> n >> k; 22 | int candy[n+5]; 23 | for(int i=0;i> candy[i]; 25 | 26 | sort(candy,candy+n); 27 | 28 | int l=0,h=n-1; 29 | int mn=0,mx=0; 30 | while(l <= h) 31 | { 32 | mn += candy[l++]; 33 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | 15 | int check(int a[],int n,int k) 16 | { 17 | int hash_rem[k+2]; 18 | memset(hash_rem,0,sizeof(hash_rem)); 19 | for(int i=0;i> t; 50 | while(t--) 51 | { 52 | int n; 53 | cin >> n ; 54 | int ans = 1; 55 | int a[n+5]; 56 | for(int i=0;i> a[i]; 58 | int k ; 59 | cin >> k; 60 | if(n%2) 61 | { 62 | cout << "False" << endl; 63 | continue; 64 | } 65 | ans = check(a,n,k); 66 | if(ans) 67 | cout << "True" < 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,m; 21 | cin >> n >> m; 22 | map mp; 23 | int a[n+5],b[n+5]; 24 | for(int i=0;i> a[i]; 27 | mp[a[i]]=1; 28 | } 29 | int flag = 1; 30 | for(int i=0;i> b[i]; 33 | if(mp.find(b[i]) == mp.end()) 34 | flag = 0; 35 | 36 | } 37 | if(flag) 38 | cout << "Yes"< 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int hash[260]={0}; 21 | string str; 22 | cin >> str; 23 | if(str.size() <= 2) 24 | { 25 | cout << 1 << endl; 26 | continue; 27 | } 28 | for(int i=0;i 0) 36 | { 37 | hash[i]--; 38 | set s; 39 | for(int j=0;j<260;j++) 40 | { 41 | if(hash[j] > 0) 42 | { 43 | s.insert(hash[j]); 44 | } 45 | } 46 | if(s.size() == 1) 47 | { 48 | cout << 1 << endl; 49 | found=1; 50 | break; 51 | } 52 | hash[i]++; 53 | s.clear(); 54 | } 55 | } 56 | if(!found) 57 | cout << 0 << endl; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Hashing/check_two_array_equal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,ans=1; 21 | cin >> n; 22 | int a[n+5],b[n+5]; 23 | int h1[10005]={0},h2[10005]={0}; 24 | for(int i=0;i> a[i]; 27 | h1[a[i]]++; 28 | } 29 | for(int i=0;i> b[i]; 32 | h2[b[i]]++; 33 | } 34 | for(int i=0;i<=10000;i++) 35 | { 36 | if(h1[i] != h2[i]) 37 | { 38 | ans = 0; 39 | break; 40 | } 41 | } 42 | cout << ans << endl; 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Hashing/count_distinct_in_every_window.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | 9 | void countDistinct(int A[], int k, int n) 10 | { 11 | map mp; 12 | int count = 0; 13 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,m,x,flag=1; 21 | cin >> n >> m >> x; 22 | int a[n+5],b[m+5]; 23 | map ma,mb; 24 | for(int i=0;i> a[i]; 27 | ma[a[i]]++; 28 | } 29 | for(int i=0;i> b[i]; 32 | mb[b[i]]++; 33 | } 34 | vector< pair< int,int> > v; 35 | for(int i=0;i 0) 40 | { 41 | mb[x-a[i]]--; 42 | flag = 0; 43 | v.push_back(make_pair(a[i],x-a[i])); 44 | } 45 | } 46 | } 47 | sort(v.begin(),v.end()); 48 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s; 21 | cin >> s; 22 | int hash[260] = {0},found=1; 23 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,k; 21 | cin >> n >> k; 22 | map mp; 23 | int a[n+5]; 24 | for(int i=0;i> a[i]; 27 | mp[a[i]]++; 28 | } 29 | int ans = -1; 30 | for(int i=0;i sums; 11 | int sum = 0,ans = 0; 12 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string str,patt; 21 | cin >> str >> patt; 22 | int hash[260]; 23 | int ans = INT_MAX; 24 | memset(hash,-1,sizeof(hash)); 25 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string P,Q; 21 | cin >> Q >> P; 22 | int p = P.size(); 23 | int q = Q.size(); 24 | int need[256] = {0}; 25 | 26 | for(int i=0;i need[Q[i]]) 46 | { 47 | if(found[Q[i]] > need[Q[i]]) 48 | found[Q[i]]--; 49 | i++; 50 | } 51 | int wlen = j-i+1; 52 | if(minWin > wlen) 53 | { 54 | start = i; 55 | end = j; 56 | minWin = wlen; 57 | } 58 | } 59 | } 60 | if(start == -1) 61 | cout << -1 << endl; 62 | else 63 | { 64 | for(int i=start;i<=end;i++) 65 | cout << Q[i]; 66 | cout << endl; 67 | } 68 | 69 | } 70 | } -------------------------------------------------------------------------------- /Hashing/swapping_pair_make_equal_sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,m; 21 | cin >> n >> m; 22 | int sum1=0,sum2=0; 23 | map mp; 24 | int a1[n]; 25 | int a2[m];; 26 | for(int i=0;i> a1[i]; 29 | mp[a1[i]] = 1; 30 | sum1 += a1[i]; 31 | } 32 | for(int i=0;i> a2[i]; 35 | sum2 += a2[i]; 36 | } 37 | if((sum1 + sum2)%2 == 0) 38 | { 39 | int found = -1; 40 | int sum = (sum1+sum2)/2; 41 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s1,s2; 21 | int h1[26] = {0}; 22 | int h2[26] = {0}; 23 | cin >> s1; 24 | cin >> s2; 25 | for(int i=0;i 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | 13 | int gcd(int a,int b) 14 | { 15 | if(b == 0) 16 | return a; 17 | else 18 | return gcd(b,a%b); 19 | } 20 | int lcm(int a ,int b) 21 | { 22 | int g = gcd(a,b); 23 | return (a*b)/g ; 24 | } 25 | int main() 26 | { 27 | fast_io; 28 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 29 | { 30 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 31 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 32 | } 33 | int a,b; 34 | cin >> a >> b; 35 | 36 | cout << gcd(a,b) << endl; 37 | cout << lcm(a,b) << endl; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Mathematical/lcm_array.cpp: -------------------------------------------------------------------------------- 1 | /* Jatin Goel 2 | IIIT Allahabad*/ 3 | /* There is no substitute of hardwork. */ 4 | /* Hardwork definitely pays off. */ 5 | /* There is no shortcut to success. */ 6 | #include 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int gcd(int a,int b) 13 | { 14 | if(b==0) 15 | return a; 16 | else 17 | return gcd(b,a%b); 18 | } 19 | int lcm_array(int a[],int n) 20 | { 21 | int ans = a[0]; 22 | 23 | for(int i=1;i> n; 39 | int a[n+5]; 40 | for(int i=0;i> a[i]; 42 | 43 | cout << lcm_array(a,n) << endl; 44 | } 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## =========Must-Do-Coding-Questions=========== 2 | 3 | ### Array 4 | 1.Largest Sum Contiguous Subarray [Kadane’s Algorithm] 5 | 2.Missing Number 6 | 3.SubArray Given Sum 7 | 4.Sort an Array of 0,1,2 8 | 5.Equilibrium Point 9 | 6.Maximum sum increasing Subsequence 10 | 7.Leaders in the Array 11 | 8.Minimum Platforms 12 | 9.Maximum of all Subarrays of size k 13 | 10.Reverse Array in Groups of size k 14 | 11.Kth Smallest Element 15 | 12.Trapping Rain Water 16 | 13.Check for Pythagorean Triplet in Array 17 | 14.Chocolate Distribution Problem 18 | 15.Stock Buy and Sell 19 | 16.Element with left side smaller and right side greater 20 | 17.Convert Array into Zig Zag Fashion 21 | 18.Find the element that appears once in sorted array 22 | 19.Kth Largest Element in stream 23 | 20.Relative Sorting 24 | 21.Spirally traversing a Matrix 25 | 22.Sorting Elements of an Array by Frequency 26 | 23.Largest Number formed from an Array 27 | 28 | ### String 29 | 1.Parenthesis Checker 30 | 2.Reverse Words in String 31 | 3.Print All Permutations of given string 32 | 4.Longest Palindromic Substring 33 | 5.Recursively remove All duplicates 34 | 6.Check if String is rotated by two Places 35 | 7.Convert Roman to Integer 36 | 8.Anagrams 37 | 9.Longest Common Substring 38 | 10.Remove Duplicates 39 | 11.Implement atoi 40 | 12.Implement strstr 41 | 13.Minimum Insertions to form Palindrome 42 | 14.Longest Common Prefix in an Array of strings [Using Binary Search] 43 | 44 | ### Hashing 45 | 1.Largest subarray with 0 sum 46 | 2.Swapping Pairs make equal sum of two Arrays 47 | 3.Count distinct elements in every window 48 | 4.Array Pair Sum Divisibility Problem 49 | 5.Minimum indexed Character 50 | 6.Find first repeated character 51 | 7.Check if two Arrays are equal or not? 52 | 8.Uncommon Characters 53 | 9.Check if frequencies can be equal 54 | 10.First element to occur k times 55 | 11.Find all pairs with a given sum 56 | 12.Array Subset of another array 57 | 13.Smallest window in a string containing all the characters of another string 58 | 59 | ### Graph Algorithms 60 | 1. Breadth First Search 61 | 2. Depth First Search 62 | 3. Count Number of Islands 63 | 4. Dijkstra [Shortest Path] 64 | 5. Find whether path exist 65 | 6. Shortest Path to Destination 66 | 7. Flood fill Algorithm 67 | 8. Minimum Cost Path 68 | 69 | ### Bit Magic 70 | 1.First Set Bit 71 | 2.Righmost Different Bit in two Numbers 72 | 3.Check kth bit Set or Not 73 | 4.Toggle Bits in given Range 74 | 5.Set kth Bit 75 | 6.Check Power of 2 76 | 7.Bit Difference 77 | 8.Number is sparse or not 78 | 9.Swap all odd and even bits 79 | 10.Count Total Set Bits 80 | 11.Longest Consecutive 1's 81 | 82 | ### Divide and Conquer 83 | 1. Binary Search iterative 84 | 2. Binary Search Recursive 85 | 3. Merge Sort 86 | 4. Quick Sort 87 | 5. K-th element of two sorted Arrays 88 | 89 | ### Dynamic Programming 90 | 1. Fibbonaci Series 91 | 2. Binomial Coefficient 92 | 3. Longest Common Subsequence 93 | 4. Maximum size square sub-matrix with all 1's 94 | 5. 0-1 Knapsack 95 | 6. Minimum Number of Jumps 96 | 7. Edit Distance 97 | 8. Longest Increasing Subsequence 98 | 9. Coin Change 99 | 10. Maximum weighted Path in Matrix to reach Last Row 100 | 11. Subset Sum Problem 101 | 12. Cutted Segments 102 | 13. Box Stacking 103 | 14. Count Number of Ways 104 | 15. Number of Paths 105 | 16. Special Keyboard 106 | 107 | ### Greedy Algorithms 108 | 1. Activity Selection 109 | 2. N meetings in one room 110 | 3. Minimum Number of Coins for Change 111 | 4. Maximize Toys 112 | 5. PageFaults in LRU 113 | 6. Largest Number Possible 114 | 7. Minimize the Sum of Product [Dot Product] 115 | 8. Minimize Heights of Tower 116 | 9. Minimum Operations to Reach N 117 | 10. Shop in Candy Store 118 | 11. Maximum Length Chain 119 | 12. Minimum Spanning Tree 120 | 13. Geek Collects the Ball 121 | 122 | ### Stack and Queue 123 | 1. First Non Repeating Character in Stream 124 | 2. Rotten Oranges 125 | 3. Next Larger Element 126 | 4. Stock Span Problem 127 | 5. Celebrity Problem 128 | 6. Number Following Pattern 129 | 7. Print Bracket Number in Expression 130 | 131 | ### Searching 132 | 1. Linear Search 133 | 134 | ### Sorting 135 | 1. Bubble Sort 136 | 2. Selection Sort 137 | 3. Insertion Sort 138 | 4. Counting Sort 139 | 140 | ### Mathematical Algorithms 141 | 1. GCD & LCM 142 | 2. LCM of Array 143 | 144 | -------------------------------------------------------------------------------- /Searching/linear_search.cpp: -------------------------------------------------------------------------------- 1 | /* Jatin Goel 2 | IIIT Allahabad*/ 3 | /* There is no substitute of hardwork. */ 4 | /* Hardwork definitely pays off. */ 5 | /* There is no shortcut to success. */ 6 | #include 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int linear_search(int a[],int n,int x) 13 | { 14 | int idx =-1; 15 | for(int i=0;i> n; 35 | int a[n+5]; 36 | for(int i=0;i> a[i]; 38 | cin >> x; 39 | int idx = linear_search(a,n,x); 40 | if(idx==-1) 41 | cout << "Element not found!"< 16 | using namespace std; 17 | 18 | 19 | // } Driver Code Ends 20 | 21 | 22 | int findMissing(int arr[], int n){ 23 | for(int i=0;i 0 && (abs(arr[i])-1) < n) 25 | arr[abs(arr[i])-1] = - arr[abs(arr[i])-1]; 26 | } 27 | for(int i=0;i0){ 29 | return i+1; 30 | } 31 | } 32 | return n+1; 33 | } 34 | // Functio to find first smallest positive 35 | // missing number in the array 36 | int missingNumber(int arr[], int n) { 37 | 38 | // Your code here 39 | int j=0; 40 | for(int i=0;in){ 42 | swap(arr[j],arr[i]); 43 | j++; 44 | } 45 | } 46 | return findMissing(arr+j,n-j); 47 | 48 | 49 | } 50 | 51 | // { Driver Code Starts. 52 | 53 | int missingNumber(int arr[], int n); 54 | 55 | int main() { 56 | int t; 57 | cin>>t; 58 | while(t--){ 59 | int n; 60 | cin>>n; 61 | int arr[n]; 62 | for(int i=0; i>arr[i]; 63 | cout< 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int bubble_sort(int a[],int n) 13 | { 14 | for(int i=0;i a[j+1]) 19 | { 20 | int temp = a[j]; 21 | a[j] = a[j+1]; 22 | a[j+1] = temp; 23 | } 24 | } 25 | } 26 | } 27 | int main() 28 | { 29 | fast_io; 30 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 31 | { 32 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 33 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 34 | } 35 | int n; 36 | cin >> n; 37 | int a[n+5]; 38 | for(int i=0;i> a[i]; 40 | bubble_sort(a,n); 41 | for(int i=0;i 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int counting_sort(int a[],int n) 13 | { 14 | int hash[10005]={0}; 15 | for(int i=0;i> n; 37 | int a[n+5]; 38 | for(int i=0;i> a[i]; 40 | counting_sort(a,n); 41 | for(int i=0;i 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int insertion_sort(int a[],int n) 13 | { 14 | for(int i=0;i 0 && temp < a[j-1]) 19 | { 20 | a[j] = a[j-1]; 21 | j--; 22 | } 23 | a[j]=temp; 24 | } 25 | } 26 | int main() 27 | { 28 | fast_io; 29 | if(fopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r")) 30 | { 31 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/input.txt", "r", stdin); 32 | freopen("C:/Users/Jatin Goel/Desktop/Algo-DS-Kit/output.txt","w",stdout); 33 | } 34 | int n; 35 | cin >> n; 36 | int a[n+5]; 37 | for(int i=0;i> a[i]; 39 | insertion_sort(a,n); 40 | for(int i=0;i 7 | using namespace std; 8 | #define LL long long 9 | #define F first 10 | #define S second 11 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 12 | int selection_sort(int a[],int n) 13 | { 14 | for(int i=0;i> n; 42 | int a[n+5]; 43 | for(int i=0;i> a[i]; 45 | selection_sort(a,n); 46 | for(int i=0;i 9 | using namespace std; 10 | int main() 11 | { 12 | int t; 13 | cin >> t; 14 | while(t--) 15 | { 16 | string s; 17 | cin >> s; 18 | stack st; 19 | int num = 0; 20 | for(int i=0;i s; 10 | 11 | int C; 12 | for (int i = 0; i < n; i++) 13 | s.push(i); 14 | 15 | int A = s.top(); 16 | s.pop(); 17 | int B = s.top(); 18 | s.pop(); 19 | 20 | while (s.size() > 1) 21 | { 22 | if (M[A][B]) 23 | { 24 | A = s.top(); 25 | s.pop(); 26 | } 27 | else 28 | { 29 | B = s.top(); 30 | s.pop(); 31 | } 32 | } 33 | 34 | C = s.top(); 35 | s.pop(); 36 | 37 | if (M[C][B]) 38 | C = B; 39 | 40 | if (M[C][A]) 41 | C = A; 42 | 43 | for (int i = 0; i < n; i++) 44 | { 45 | if ( (i != C) && (M[C][i] || !M[i][C] )) 46 | return -1; 47 | } 48 | 49 | return C; 50 | } -------------------------------------------------------------------------------- /Stack-Queue/first_non_repeating_character.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int a[256] ={0}; 21 | int n,i; 22 | cin >> n; 23 | char c[n]; 24 | queue < char> q; 25 | for(i=0;i> c[i]; 28 | a[c[i]]++; 29 | if(a[c[i]] == 1) 30 | q.push(c[i]); 31 | while(!q.empty() && a[q.front()] > 1) 32 | { 33 | q.pop(); 34 | } 35 | if(q.empty()) 36 | cout << -1 << " "; 37 | else 38 | cout << q.front() << " "; 39 | } 40 | cout << endl; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Stack-Queue/next_larger_element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | #include 15 | using namespace std; 16 | int main() 17 | { 18 | int t; 19 | cin >> t; 20 | while(t--) 21 | { 22 | int n; 23 | stack mystack; 24 | cin >> n; 25 | int arr[n],ans[n]; 26 | for(int i = 0;i < n;++i) 27 | ans[i] = -1; 28 | for(int i = 0;i < n;++i) 29 | cin >> arr[i]; 30 | for(int i = 0;i < n;++i) 31 | { 32 | while(!mystack.empty() && arr[i] > arr[mystack.top()]) 33 | { 34 | int a = mystack.top(); 35 | mystack.pop(); 36 | ans[a] = arr[i]; 37 | } 38 | mystack.push(i); 39 | } 40 | for(int i = 0;i < n;++i) 41 | cout << ans[i] << " "; 42 | 43 | cout << endl; 44 | } 45 | } -------------------------------------------------------------------------------- /Stack-Queue/number_following_pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | int main() 11 | { 12 | int t; 13 | cin >> t; 14 | while(t--) 15 | { 16 | string s; 17 | cin >> s; 18 | stack st; 19 | string ans = ""; 20 | for(int i=0;i<=s.size();i++) 21 | { 22 | st.push(i+1); 23 | 24 | if(s[i] == 'I' || s.size() == i) 25 | { 26 | while(!st.empty()) 27 | { 28 | ans = ans + to_string(st.top()); 29 | st.pop(); 30 | } 31 | } 32 | } 33 | cout << ans << endl; 34 | ans.clear(); 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Stack-Queue/rotten_oranges.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | int n,m; 21 | cin >> n >> m; 22 | int grid[n+5][m+5]; 23 | int visited[n+5][m+5]; 24 | memset(visited,0,sizeof(visited)); 25 | queue < pair > q; 26 | for(int i=0;i> grid[i][j]; 31 | if(grid[i][j] == 2) 32 | { 33 | q.push(make_pair(i,j)); 34 | visited[i][j] = 1; 35 | } 36 | } 37 | } 38 | int ans = 0; 39 | while(!q.empty()) 40 | { 41 | vector< pair > v; 42 | int flag = 0; 43 | while(!q.empty()) 44 | { 45 | pair temp = q.front(); 46 | q.pop(); 47 | int x = temp.F; 48 | int y = temp.S; 49 | int dir[4][2] = {{0,1},{1,0},{-1,0},{0,-1}}; 50 | for(int i=0;i<4;i++) 51 | { 52 | int p = x + dir[i][0]; 53 | int r = y + dir[i][1]; 54 | if(p >= 0 && p < n && r >= 0 && r < m) 55 | { 56 | if(visited[p][r] == 0 && grid[p][r] == 1) 57 | { 58 | flag = 1; 59 | v.push_back(make_pair(p,r)); 60 | visited[p][r] = 1; 61 | } 62 | } 63 | } 64 | } 65 | for(int i=0;i 9 | using namespace std; 10 | int main() 11 | { 12 | int t; 13 | cin >> t; 14 | while(t--) 15 | { 16 | int n; 17 | cin >> n; 18 | int price[n+5],Span[n+5]; 19 | for(int i=0;i> price[i]; 22 | } 23 | stack st; 24 | st.push(0); 25 | Span[0] = 1; 26 | for(int i=1;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | 15 | vector permutations; 16 | 17 | void permute(string s,int start,int end) 18 | { 19 | if(start == end) 20 | { 21 | permutations.push_back(s); 22 | return; 23 | } 24 | else 25 | { 26 | for(int i=start;i<=end;i++) 27 | { 28 | char temp = s[start]; 29 | s[start] = s[i]; 30 | s[i] = temp; 31 | permute(s,start+1,end); 32 | //backtrack; 33 | temp = s[i]; 34 | s[i] = s[start]; 35 | s[start] = temp; 36 | } 37 | } 38 | } 39 | int main() 40 | { 41 | int t; 42 | cin >> t; 43 | while(t--) 44 | { 45 | string s; 46 | cin >> s; 47 | int start = 0,end = s.size()-1; 48 | permute(s,start,end); 49 | sort(permutations.begin(),permutations.end()); 50 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s1,s2; 21 | cin >> s1 >> s2; 22 | int h[28] = {0},g[28] = {0}; 23 | for(int i=0;i= '0' && str[i] <= '9') 21 | { 22 | num = num*10 + str[i] - '0'; 23 | } 24 | else 25 | { 26 | flag = 0; 27 | } 28 | } 29 | if(flag) 30 | { 31 | return num*m; 32 | } 33 | return -1; 34 | } 35 | -------------------------------------------------------------------------------- /String/implement_strstr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | 9 | int strstr(string s, string x) 10 | { 11 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int minLen(vector v) 15 | { 16 | int len = INT_MAX; 17 | for(int i=0;i v,int mid) 25 | { 26 | for(int i=0;i> t; 40 | while(t--) 41 | { 42 | int n; 43 | cin >> n; 44 | vector v; 45 | for(int i=0;i> s; 49 | v.push_back(s); 50 | } 51 | int minlen = minLen(v); 52 | int l=1,h=minlen,ans=-1; 53 | while(l<=h) 54 | { 55 | int mid = (l+h)/2; 56 | if(check(v,mid)) 57 | { 58 | l = mid+1; 59 | ans = mid; 60 | } 61 | else 62 | { 63 | h = mid -1; 64 | } 65 | } 66 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | 15 | int LCStr(string s1,string s2,int m,int n) 16 | { 17 | int dp[m+1][n+1]; 18 | int result = 0; 19 | for(int i=0;i<=m;i++) 20 | { 21 | for(int j=0;j<=n;j++) 22 | { 23 | if(i==0 || j==0) 24 | dp[i][j] = 0; 25 | else if(s1[i-1] == s2[j-1]) 26 | { 27 | dp[i][j] = dp[i-1][j-1] + 1; 28 | result = max(result,dp[i][j]); 29 | } 30 | else 31 | dp[i][j] = 0; 32 | 33 | } 34 | } 35 | return result; 36 | } 37 | int main() 38 | { 39 | int t; 40 | cin >> t; 41 | while(t--) 42 | { 43 | string s1,s2; 44 | int m,n; 45 | cin >> m >> n; 46 | cin >> s1 >> s2; 47 | cout << LCStr(s1,s2,m,n) << endl; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /String/longest_palindromic_substring.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s; 21 | cin >> s; 22 | int l,h; 23 | int maxAns = -1; 24 | int start = -1,end=-1; 25 | int n = s.size(); 26 | for(int i=1;i= 0 && h < n && s[l] == s[h]) 32 | { 33 | if(h-l + 1 > maxAns) 34 | { 35 | maxAns = h-l+1; 36 | start = l; 37 | end = h; 38 | } 39 | l--; 40 | h++; 41 | } 42 | //Odd Length 43 | l=i-1; 44 | h=i+1; 45 | while(l >= 0 && h < n && s[l] == s[h]) 46 | { 47 | if(h-l + 1 > maxAns) 48 | { 49 | maxAns = h-l+1; 50 | start = l; 51 | end = h; 52 | } 53 | l--; 54 | h++; 55 | } 56 | } 57 | if(maxAns == -1) 58 | { 59 | start = 0; 60 | end = 0; 61 | } 62 | for(int i=start;i<=end;i++) 63 | cout << s[i]; 64 | cout << endl; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /String/min_insertions_to_form_palindrome.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | 15 | int LCS(string s1,string s2,int m,int n) 16 | { 17 | int dp[m+1][n+1]; 18 | int result = 0; 19 | for(int i=0;i<=m;i++) 20 | { 21 | for(int j=0;j<=n;j++) 22 | { 23 | if(i==0 || j==0) 24 | dp[i][j] = 0; 25 | else if(s1[i-1] == s2[j-1]) 26 | { 27 | dp[i][j] = dp[i-1][j-1] + 1; 28 | result = max(result,dp[i][j]); 29 | } 30 | else 31 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 32 | 33 | } 34 | } 35 | return result; 36 | } 37 | int main() 38 | { 39 | int t; 40 | cin >> t; 41 | while(t--) 42 | { 43 | string s1,s2; 44 | int n; 45 | cin >> s1; 46 | n = s1.size(); 47 | s2 = s1; 48 | reverse(s2.begin(),s2.end()); 49 | cout << n - LCS(s1,s2,n,n) << endl; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /String/parenthesis_checker.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s; 21 | cin >> s; 22 | stack st; 23 | int n = s.size(); 24 | int flag = 1; 25 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s; 21 | cin >> s; 22 | stack st; 23 | int n = s.size(); 24 | int i = 0; 25 | while(i Ans; 44 | while(!st.empty()) 45 | { 46 | Ans.push(st.top()); 47 | st.pop(); 48 | } 49 | while(!Ans.empty()) 50 | { 51 | cout << Ans.top() ; 52 | Ans.pop(); 53 | } 54 | cout << endl; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /String/remove_duplicates.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | string s; 19 | getline(cin,s); 20 | while(t--) 21 | { 22 | int hash[260] = {0}; 23 | string s; 24 | getline(cin,s); 25 | int n = s.size(); 26 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string s; 21 | stack st; 22 | cin >> s; 23 | string tmp; 24 | tmp=""; 25 | int ctr=0; 26 | for(int i=0;i 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int value(char r) 15 | { 16 | if (r == 'I') 17 | return 1; 18 | if (r == 'V') 19 | return 5; 20 | if (r == 'X') 21 | return 10; 22 | if (r == 'L') 23 | return 50; 24 | if (r == 'C') 25 | return 100; 26 | if (r == 'D') 27 | return 500; 28 | if (r == 'M') 29 | return 1000; 30 | 31 | return -1; 32 | } 33 | int romanToDecimal(string str) 34 | { 35 | int res = 0; 36 | for (int i=0; i= s2) 45 | { 46 | res = res + s1; 47 | } 48 | else 49 | { 50 | res = res + s2 - s1; 51 | i++; 52 | } 53 | } 54 | else 55 | { 56 | res = res + s1; 57 | i++; 58 | } 59 | } 60 | return res; 61 | } 62 | int main() 63 | { 64 | int t; 65 | cin >> t; 66 | while(t--) 67 | { 68 | string str; 69 | cin >> str; 70 | cout << romanToDecimal(str) << endl; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /String/string_rotated_by_two_places.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define fast_io ios::sync_with_stdio(false);cin.tie(NULL) 14 | int main() 15 | { 16 | int t; 17 | cin >> t; 18 | while(t--) 19 | { 20 | string a,b,a1,a2; 21 | cin >> a ; 22 | a1=""; 23 | a2=""; 24 | cin >> b; 25 | int l = a.size(); 26 | a1 = a1 + a.substr(2) + a.substr(0,2); 27 | a2 = a2 + a.substr(l-2) + a.substr(0,l-2); 28 | if(a1 == b || a2 == b) 29 | cout << 1 << endl; 30 | else 31 | cout << 0 << endl; 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 4 0 2 5 -3 -------------------------------------------------------------------------------- /output.txt: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /template.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | @Author - Jatin Goel 3 | @Institute - IIIT Allahabad 4 | Hardwork definitely pays off. 5 | There is no substitute of hardwork. 6 | There is no shortcut to success. 7 | */ 8 | #include 9 | using namespace std; 10 | #define LL long long 11 | #define F first 12 | #define S second 13 | #define PII pair 14 | 15 | int main() 16 | { 17 | int t; 18 | cin >> t; 19 | while(t--) 20 | { 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /template.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/de-cryptor/Must-Do-Coding-Questions/857a96fe20ae6c51a517b105988416362d10aadd/template.exe --------------------------------------------------------------------------------