├── Arrays ├── tempCodeRunnerFile.cpp ├── min_coin_change_not_possible.cpp ├── Kadens_algo.cpp ├── Buy_and_sell_one_stock_simple.cpp ├── count_subarrays_with_given_XOR.cpp ├── removing_min_and_max.cpp ├── Remove_duplicates_inPlace_constant_extra_space.cpp ├── Rotate_matrix_90_deg.cpp ├── first_duplicate.cpp ├── House_robber.cpp ├── merge_array_with_no_extra_space.cpp ├── Longest_subarray_with_sum_zero.cpp ├── Pow_X_N_imp.cpp ├── first_non_repeating_char.cpp ├── DNF_sort_0_1_2.cpp ├── DAYS_stock_buy_sell_MAX_profit.cpp ├── fitting_array.cpp ├── city_sceduling.cpp ├── printchangetut_2.6.2.cpp ├── small_rope.cpp ├── platforms.cpp ├── smallest_missing_positive_number_IMP.cpp ├── chocolate_distribution(min_diff).cpp ├── k_radius_subarray.cpp ├── min_swaps_to_bring_k_togerther.cpp ├── Longest_consecutive_sequence.cpp ├── reduce_array(PAIRS).cpp ├── finding_partition_element.cpp ├── Array_permutations_easy_way.cpp ├── num_of_subarrays_having_prod_less_than_k.cpp ├── pascal_triangle.cpp ├── next_permutation.cpp ├── moores_voting_algo.cpp ├── Get_a_sum_in_four_arrays_IMP.cpp ├── Continuous_Subarray_RunningSum.cpp ├── pairs_divisible_by_K_in_Arr_gcd.cpp ├── Finding_target_indices_after_sorting_array.cpp ├── min_homecoing_cost_grid.cpp ├── Sum_beauty_of_all_subarrays.cpp ├── merge_overlapping_intervals.cpp ├── two_sum.cpp ├── min_jumps_to_end.cpp ├── Arthimetic_silces_in_Array.cpp └── kth_smallest_in_arr.cpp ├── FINAL450.pdf ├── Strings ├── LPS_of_str.exe ├── moditify_string.exe ├── min_chars_to_insert_at_begin_to_make_it_palindrome_IMP_LPS.exe ├── LPS_of_str.cpp ├── longest_common_prefix.cpp ├── col_base_operation.cpp ├── GCD_of_strings_IMp.cpp ├── min_chars_to_insert_at_begin_to_make_it_palindrome_IMP_LPS.cpp ├── Remove_duplicates_from_strings_IMP.cpp ├── max_num_at_most_K_swaps_IMP.cpp └── common_child.cpp ├── Segment Trees ├── segment_tree.exe ├── Screenshot (2).png ├── Screenshot (3).png ├── Screenshot (6).png └── tempCodeRunnerFile.cpp ├── Graphs ├── Finding_Mother_vertices.exe ├── Num_of_spanning_tree.cpp ├── Minimum_steps_to_reach_destination.cpp ├── Check_if_Bridge.cpp ├── DisJoint_Set.cpp ├── Flood_fill_algo.cpp ├── bfs_array.cpp ├── BFS_graph_travrse.cpp ├── Rectangle_mania_IMP.cpp ├── int1.cpp └── component_detection.cpp ├── Dynamic-Programming ├── DP_CP_K_OrderLCS.exe ├── Target_Sum.cpp ├── longest_palindromic_subsequence(Dynamic_programing).o ├── friends_paring_DP.cpp ├── tempCodeRunnerFile.cpp ├── Catlan_nums_Dp.cpp ├── Optimal_game_strategy_Dp.cpp ├── Knapsack_0-N_DP.cpp ├── Ugly-nums_DP.cpp ├── Binomial_CoefficientProblem_DP.cpp ├── min_num_of_jumps(DYNAMIC).cpp ├── number_of_steps(efficent by dynmic programing).cpp ├── max_sum_partition.cpp ├── subset_sum-dynmic.cpp ├── Bitonic_subsequence_max_len_DP.cpp ├── vikingString.cpp ├── longest_wiggle_subsequnce_IMP.cpp ├── longest_common_subsequence(DYNAMIC).cpp ├── Ballon_Burst.cpp ├── longest_palindromic_subsequence(Dynamic_programing).cpp ├── Deletions_required_to_make_palindrome(Dynamic).cpp ├── MaxSumRectangle_In_Mat.cpp ├── Buy_and_sell_stock_with_transaction_Fee.cpp ├── coin_exchang_DYNAMIC.cpp └── DP_CP_K_OrderLCS.cpp ├── Link-Lists ├── ss.cpp ├── Delete_Nodes_having_values_greater_at_right.cpp ├── Find_middle_of_LL.cpp ├── Multiply_two_nums_represeted_by_LL.cpp ├── Nth_node_from_last.cpp ├── Flatten_a_LL_levelwise.cpp ├── Remove_duplicates_in_sorted_LL.cpp ├── sort_LL_of_0s_1s_2s.cpp ├── Intersection_point_of_two_LL.cpp ├── Check_if_list_is_palindrome.cpp └── Flatten_a_LL_in_sorted_manner.cpp ├── Searching and sorting ├── Kth_smallest_element_in_sorted_two_arrays.exe ├── cpy.cpp ├── Optimal_location_of_point_to_minimize_total_dist_Ternary_search.exe ├── visfiwarjgwi.cpp ├── arr_end_ptr.cpp ├── Find_max_sum_such_that_no_two_ele_are_adjacents.cpp ├── Remove_duplicates_inPlace_constant_extra_space.cpp ├── Square_root_of_interger.cpp ├── count_tripltes_with_sum_smaller_than_X.cpp ├── Product_arr_puzzle.cpp ├── Insertion_sort.cpp ├── Find_missing_and_repeating_ele.cpp ├── Selection_sort.cpp ├── DnF_sort(0,1,2).cpp ├── Bubble_sort.cpp ├── Missing_term_in_AP.cpp ├── Median_of_rowise_sorted_matrix.cpp ├── sort_array_according_to_num_of_set_bits.cpp ├── Find_majority_ele_moor_voting.cpp ├── book_allocation(minimize_max_pages).cpp ├── search_in_array_where_adjacent_eles_differ_by_K.cpp ├── Search_in_sort_rotated_array.cpp ├── Aggresive_cows.cpp ├── Kth_smallest_pair_distance.cpp └── Nth_magical_num_Bin_search.cpp ├── Heaps ├── tp.c ├── Laptop_rentenals.cpp ├── sort_k_sorted_arrays.cpp ├── conncect_n_ropes_optimal_pattern_sum.cpp └── Reorganize_strings_leetcode.cpp ├── Stacks and Queues ├── bracket_crrt.cpp ├── Interleave_the_ele_of_queue.cpp ├── Reverse_Queue_recursively.cpp ├── LIS_in_NlogN.cpp ├── Next_greater_element.cpp ├── Longest_valid_substring.cpp ├── rain_water_trapping.cpp ├── Celebrity_party_problem.cpp ├── Merge_overlapping_intervals.cpp ├── minimu_sum_of_squares_of_char_counts_after_removing_k.cpp ├── Steps_to_make_arr_Non_decreasing_MonoTonciStack.cpp ├── Sort_stack_by_recursion.cpp ├── Reverse_stack_recursively.cpp ├── Largest_area_in_histogram.cpp ├── postfix_Evaluation.cpp ├── get_min_stack_in_space_efficient.cpp ├── Trapping_Rainwater.cpp └── Max_score_of_good_subarray_VIMP_NSR_NSL.cpp ├── solving ├── sol.cpp ├── casir.cpp ├── ticks.cpp └── swap.cpp ├── Bit-Manipulation ├── compliments_bits.cpp ├── game_Of_XOR.cpp ├── BIT_Rotation.cpp ├── bit_wise_AND_pair_sum.cpp ├── Hexadecimal_to_decimal.cpp ├── sum_of_bit_difference_of_all_pairs.cpp ├── xorring_in_given_array.cpp └── bit_wise_OR_pair_sum.cpp ├── Algorithms ├── josheph's_death_circle(ADVANCED).cpp ├── josheph's_death_circle(adjacent).cpp ├── random_and_by_weights.cpp ├── Bineary_Exponentiation.cpp ├── lucky_NUM.cpp ├── Euler_totient_fun.cpp ├── Hexadecimal_to_decimal.cpp ├── sieve_of_erathosthenes.cpp ├── sieve_of_erathosthenes_SPF.cpp ├── printchangetut_2.6.2.cpp ├── small_rope.cpp ├── min_swaps_to_bring_k_togerther.cpp ├── Good_array_gcd.cpp ├── setprecision.cpp ├── Median_of_rowise_sorted_matrix.cpp ├── Extended_Euclid.cpp ├── sum_divisible_by.cpp ├── moores_voting_algo.cpp ├── Multiplicative_modulo_inverse.cpp └── reduce_to_zero.cpp ├── Greedy ├── min_query_time.cpp ├── valid_starting_city.cpp ├── maximize_array_sum_in_K_negations.cpp ├── smallest_string_with_given_numeric_value.cpp ├── Adding_missing_number_in_the_patch_IMP.cpp ├── min_buckets_water.cpp ├── min_candies(GREEDY).cpp ├── activity_selection_greedy.cpp └── Tandem_speed.cpp ├── Back-Tracking and Recursion ├── wine_selling_RECURSION.cpp ├── split_perfect_squares(RECURSIVE).cpp ├── Sub_setSum.cpp ├── permutations_of_string(effective swapping).cpp ├── Form_Subset_II.cpp ├── Generate_parantheses.cpp ├── Kth_permutation_of_given_sequence.cpp ├── Word_break_by_BackTracking.cpp └── Generate_all_possible_nums.cpp ├── Divide and Conqure ├── Finding_peak_ele_1D_in_logN_time.cpp ├── Nth_root_of_number_m.cpp └── Finding_peak_ele_2D_in_MLogN_time.cpp ├── Binary Search Trees ├── Populate_inorder_successors.cpp ├── Find_closests_val_in_BST_to_target.cpp ├── All_branch_sums.cpp ├── Kth_largest_in_BST.cpp ├── Check_if_given_preorder_represents_BST.cpp ├── Convert_normalBST_to_balanced_BST.cpp ├── Three_nodes_in_BST.cpp └── LCA_in_BST.cpp └── Trees ├── Invert_binary_tree.cpp ├── Find_Depth_sums.cpp ├── Flatten_tree_into_LL.cpp ├── Left_view_of_BT.cpp ├── Right_view_of_BT.cpp ├── Compare_leaf_Traverse.cpp ├── Reverse_level_order_traversal.cpp ├── Bottom_view_of_BT.cpp ├── All_branch_sums.cpp ├── BT_to_sumTree.cpp ├── Diagonal_Traversal_of_a_BT.cpp ├── Check_if_Trees_are_height_balanced.cpp └── max_diff_between_node_and_ancestor_imp_rec.cpp /Arrays/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /FINAL450.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/FINAL450.pdf -------------------------------------------------------------------------------- /Strings/LPS_of_str.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Strings/LPS_of_str.exe -------------------------------------------------------------------------------- /Strings/moditify_string.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Strings/moditify_string.exe -------------------------------------------------------------------------------- /Segment Trees/segment_tree.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Segment Trees/segment_tree.exe -------------------------------------------------------------------------------- /Segment Trees/Screenshot (2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Segment Trees/Screenshot (2).png -------------------------------------------------------------------------------- /Segment Trees/Screenshot (3).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Segment Trees/Screenshot (3).png -------------------------------------------------------------------------------- /Segment Trees/Screenshot (6).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Segment Trees/Screenshot (6).png -------------------------------------------------------------------------------- /Graphs/Finding_Mother_vertices.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Graphs/Finding_Mother_vertices.exe -------------------------------------------------------------------------------- /Dynamic-Programming/DP_CP_K_OrderLCS.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Dynamic-Programming/DP_CP_K_OrderLCS.exe -------------------------------------------------------------------------------- /Link-Lists/ss.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int x=1; 8 | cout<<#x<<" = "< 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | 8 | int main() 9 | { 10 | 11 | } -------------------------------------------------------------------------------- /Searching and sorting/Kth_smallest_element_in_sorted_two_arrays.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Searching and sorting/Kth_smallest_element_in_sorted_two_arrays.exe -------------------------------------------------------------------------------- /Strings/min_chars_to_insert_at_begin_to_make_it_palindrome_IMP_LPS.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Strings/min_chars_to_insert_at_begin_to_make_it_palindrome_IMP_LPS.exe -------------------------------------------------------------------------------- /Dynamic-Programming/longest_palindromic_subsequence(Dynamic_programing).o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Dynamic-Programming/longest_palindromic_subsequence(Dynamic_programing).o -------------------------------------------------------------------------------- /Heaps/tp.c: -------------------------------------------------------------------------------- 1 | #include 2 | int p=0; 3 | int *g=&p; 4 | 5 | int main() 6 | { 7 | int *l=NULL; 8 | 9 | if(l==g) 10 | printf("e"); 11 | else 12 | printf("ne"); 13 | } -------------------------------------------------------------------------------- /Segment Trees/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | cout << "Ind : " << ind << "\n"; 2 | cout << "Left Right : " << left << " : " << right << "\n"; 3 | cout << "low high : " << low << " : " << high << "\n\n"; 4 | -------------------------------------------------------------------------------- /Searching and sorting/cpy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int *p=NULL; 8 | 9 | delete p; 10 | 11 | cout<<"Done\n"; 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Searching and sorting/Optimal_location_of_point_to_minimize_total_dist_Ternary_search.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onkarbartakke/DSA-Marathon/HEAD/Searching and sorting/Optimal_location_of_point_to_minimize_total_dist_Ternary_search.exe -------------------------------------------------------------------------------- /Searching and sorting/visfiwarjgwi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | string s; 6 | cin >> s; 7 | cout << s << "\n"; 8 | // stringstream ss(s); 9 | // string word = ""; 10 | // while(ss >> word){ 11 | // cout << word << "\n"; 12 | // } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Stacks and Queues/bracket_crrt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | string s; 10 | cin>>s; 11 | int l; 12 | l=s.length(); 13 | 14 | if(l%2==1 || s[0]==')' || s[l-1]=='(') 15 | cout<<"NO\n"; 16 | else 17 | cout<<"YES\n"; 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Searching and sorting/arr_end_ptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n=10; 8 | int i,j; 9 | int a[10]={2,5,8,9,6,3,1,4,7,0}; 10 | 11 | int *p=a; 12 | 13 | cout<<*(p)<<"\n"; 14 | cout<<*(p+1)<<"\n"; 15 | cout<<*(p+2)<<"\n"; 16 | cout<<*(p+n)<<"\n"; 17 | cout<<*(p+n-1)<<"\n"; 18 | 19 | 20 | } -------------------------------------------------------------------------------- /solving/sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | 6 | 7 | int main() 8 | { 9 | int a,b,c; 10 | a=5; 11 | b=4; 12 | c=6; 13 | 14 | for(c=4;c<=7;c++) 15 | { 16 | if((a&c&b) < (c-b-a)) 17 | { 18 | continue; 19 | } 20 | 21 | b=(b+9)+a; 22 | b=a^a; 23 | } 24 | 25 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int num,i,result; 6 | cin>>num; 7 | result=0; 8 | i=0; 9 | while(num) 10 | { 11 | if((num&1)==0) 12 | { 13 | result= result + (1<>1; 18 | 19 | } 20 | 21 | cout<<"1s compliment "< 2 | using namespace std; 3 | int win(int n,int k) 4 | { 5 | if(n==1) 6 | return 1; 7 | else 8 | { 9 | return (win(n-1,k)+k-1)%n+1; 10 | } 11 | } 12 | int main() { 13 | //code 14 | int t; 15 | cin>>t; 16 | while(t--) 17 | { 18 | int n,k; 19 | cin>>n>>k; 20 | int ans=win(n,k); 21 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int nonConstructibleChange(vector coins) { 7 | // Write your code here. 8 | sort(coins.begin(),coins.end()); 9 | 10 | int minCoinChange=0; 11 | 12 | for(auto i : coins) 13 | { 14 | if(minCoinChange + 1 < i) 15 | return minCoinChange +1 ; 16 | 17 | minCoinChange+=i; 18 | } 19 | return minCoinChange+1; 20 | } 21 | -------------------------------------------------------------------------------- /Arrays/Kadens_algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int maxSubArray(vector &nums) 9 | { 10 | 11 | int curr_max = nums[0], global_max = nums[0]; 12 | 13 | for (int i = 1; i < nums.size(); i++) 14 | { 15 | curr_max = max(curr_max + nums[i], nums[i]); 16 | global_max = max(global_max, curr_max); 17 | } 18 | 19 | return global_max; 20 | } 21 | }; -------------------------------------------------------------------------------- /Dynamic-Programming/friends_paring_DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | 9 | if (n == 1 || n == 2) 10 | { 11 | cout << n; 12 | } 13 | else 14 | { 15 | int dp[n + 1]; 16 | dp[1] = 1; 17 | dp[2] = 2; 18 | 19 | for (int i = 3; i <= n; i++) 20 | { 21 | dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]; 22 | } 23 | 24 | cout << dp[n]; 25 | } 26 | } -------------------------------------------------------------------------------- /Arrays/Buy_and_sell_one_stock_simple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int maxProfit(vector &prices) 9 | { 10 | 11 | int mini = INT_MAX; 12 | int maxpro = 0; 13 | 14 | for (int i = 0; i < prices.size(); i++) 15 | { 16 | mini = min(prices[i], mini); 17 | maxpro = max(maxpro, prices[i] - mini); 18 | } 19 | 20 | return maxpro; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Arrays/count_subarrays_with_given_XOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int Solve(vector &A, int B) 6 | { 7 | map freq; 8 | int cnt = 0; 9 | int xorr = 0; 10 | for (auto it : A) 11 | { 12 | xorr = xorr ^ it; 13 | 14 | if (xorr == B) 15 | cnt++; 16 | 17 | if (freq.find(xorr ^ B) != freq.end()) 18 | cnt += freq[xorr ^ B]; 19 | 20 | freq[xorr] += 1; 21 | } 22 | return cnt; 23 | } -------------------------------------------------------------------------------- /Arrays/removing_min_and_max.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int minimumDeletions(vector &nums) 9 | { 10 | 11 | int n = nums.size(); 12 | int i = min_element(nums.begin(), nums.end()) - nums.begin(); 13 | int j = max_element(nums.begin(), nums.end()) - nums.begin(); 14 | 15 | if (i > j) 16 | swap(i, j); 17 | 18 | return min(i + 1 + n - j, min(j + 1, n - i)); 19 | } 20 | }; -------------------------------------------------------------------------------- /Stacks and Queues/Interleave_the_ele_of_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /* 8 | Following are the steps to solve the problem: 9 | 1.Push the first half elements of queue to stack. 10 | 2.Enqueue back the stack elements. 11 | 3.Dequeue the first half elements of the queue and enqueue them back. 12 | 4.Again push the first half elements into the stack. 13 | 5.Interleave the elements of queue and stack. 14 | 15 | 16 | */ 17 | 18 | int main() 19 | { 20 | 21 | } -------------------------------------------------------------------------------- /Algorithms/josheph's_death_circle(adjacent).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | 5 | using namespace std; 6 | 7 | 8 | int main() 9 | { 10 | int n; 11 | cout<<"Enter number of soliders\n"; 12 | cin>>n; 13 | int count=0; 14 | int m=n; 15 | while(m!=0) 16 | { 17 | m=m/2; 18 | count++; 19 | } 20 | count--; 21 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | srand(time(0)); 7 | int n,i; 8 | cin>>n; 9 | 10 | int a[n]; 11 | for(i=0;i>a[i]; 14 | } 15 | 16 | int cumlsum[n]; 17 | cumlsum[0]=a[0]; 18 | for(i=1;i 2 | 3 | using namespace std; 4 | 5 | const int MOD=1e9+7; 6 | 7 | int Pow(int a, int n) 8 | { 9 | a=a%MOD; 10 | int ans=1; 11 | 12 | while(n>0) 13 | { 14 | if(n&1) 15 | ans=(ans*a)%MOD; 16 | 17 | 18 | a=(a*a)%MOD; 19 | 20 | n=n>>1; 21 | } 22 | 23 | return ans; 24 | } 25 | int main() 26 | { 27 | cout<<"Enter 'a' and 'n' to cal pow(a,n) : "; 28 | int a,n; 29 | cin>>a>>n; 30 | 31 | cout<<"\npow(a,n) : "< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int removeDuplicates(vector &nums) 9 | { 10 | 11 | if (nums.size() == 0) 12 | return 0; 13 | int left = 0; 14 | for (int right = 1; right < nums.size(); right++) 15 | { 16 | if (nums[left] != nums[right]) 17 | left++; 18 | nums[left] = nums[right]; 19 | } 20 | return left + 1; 21 | } 22 | }; -------------------------------------------------------------------------------- /Greedy/min_query_time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int minimumWaitingTime(vector queries) 8 | { 9 | // Write your code here.i 10 | sort(queries.begin(), queries.end()); 11 | 12 | int n = queries.size(); 13 | 14 | int sum = 0; 15 | int ans = 0; 16 | for (int i = 0; i < n - 1; i++) 17 | { 18 | sum += (queries[i]); 19 | ans += sum; 20 | } 21 | 22 | return ans; 23 | } 24 | 25 | int main() 26 | { 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Arrays/Rotate_matrix_90_deg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | void rotate(vector> &matrix) 9 | { 10 | 11 | int n = matrix.size(); 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | for (int j = 0; j < i; j++) 16 | swap(matrix[i][j], matrix[j][i]); 17 | } 18 | 19 | for (int i = 0; i < matrix.size(); i++) 20 | { 21 | reverse(matrix[i].begin(), matrix[i].end()); 22 | } 23 | } 24 | }; -------------------------------------------------------------------------------- /Searching and sorting/Find_max_sum_such_that_no_two_ele_are_adjacents.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int FindMaxSum(int arr[], int n) 6 | { 7 | // Your code here 8 | int i, j, op1, op2; 9 | 10 | int ans[n]; 11 | ans[0] = arr[0]; 12 | ans[1] = max(arr[0], arr[1]); 13 | 14 | for (i = 2; i < n; i++) 15 | { 16 | op1 = arr[i] + ans[i - 2]; 17 | op2 = ans[i - 1]; 18 | 19 | ans[i] = max(op1, op2); 20 | } 21 | 22 | return ans[n - 1]; 23 | } 24 | 25 | int main() 26 | { 27 | 28 | } -------------------------------------------------------------------------------- /Searching and sorting/Remove_duplicates_inPlace_constant_extra_space.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int removeDuplicates(vector &nums) 9 | { 10 | 11 | if (nums.size() == 0) 12 | return 0; 13 | int left = 0; 14 | for (int right = 1; right < nums.size(); right++) 15 | { 16 | if (nums[left] != nums[right]) 17 | left++; 18 | nums[left] = nums[right]; 19 | } 20 | return left + 1; 21 | } 22 | }; -------------------------------------------------------------------------------- /Greedy/valid_starting_city.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int validStartingCity(vector distances, vector fuel, int mpg) { 6 | // Write your code here. 7 | int n,i,j; 8 | n=distances.size(); 9 | int currfuel=0; 10 | int start=0; 11 | for(i=0;i=0) 16 | continue; 17 | else 18 | { 19 | currfuel=0; 20 | start=(i+1)%n; 21 | } 22 | 23 | } 24 | return start; 25 | } 26 | 27 | int main() 28 | { 29 | 30 | } -------------------------------------------------------------------------------- /Back-Tracking and Recursion/wine_selling_RECURSION.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int profit(int a[], int n, int y, int left, int right) 4 | { 5 | if (left == right) 6 | { 7 | return y * a[left]; 8 | } 9 | else 10 | { 11 | return max(a[left] * y + profit(a, n, y + 1, left + 1, right), a[right] * y + profit(a, n, y + 1, left, right - 1)); 12 | } 13 | } 14 | int main() 15 | { 16 | int n, i; 17 | cin >> n; 18 | int a[n]; 19 | for (i = 0; i < n; i++) 20 | { 21 | cin >> a[i]; 22 | } 23 | 24 | int ans = profit(a, n, 1, 0, n - 1); 25 | cout << ans; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Divide and Conqure/Finding_peak_ele_1D_in_logN_time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int findPeakElement(vector &nums) 9 | { 10 | 11 | int low = 0, high = nums.size() - 1, mid; 12 | 13 | while (low < high) 14 | { 15 | int mid1 = (low + high) / 2, mid2; 16 | mid2 = mid1 + 1; 17 | 18 | if (nums[mid1] < nums[mid2]) 19 | low = mid2; 20 | else 21 | high = mid1; 22 | } 23 | 24 | return low; 25 | } 26 | }; -------------------------------------------------------------------------------- /Arrays/first_duplicate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int firstDuplicateValue(vector a) { 7 | int n,i,j; 8 | n=a.size(); 9 | 10 | for(i=0;i>n; 28 | vectora(n); 29 | 30 | for(i=0;i>a[i]; 32 | 33 | cout<<"\n"< 2 | 3 | using namespace std; 4 | 5 | int split_perfect_squares(int n) 6 | { 7 | if(n<=3) 8 | { 9 | return n; 10 | } 11 | else 12 | { 13 | int ans=n; 14 | int i; 15 | for(i=1;i*i<=n;i++) 16 | { 17 | ans=min(ans, 1+split_perfect_squares(n-(i*i))); 18 | } 19 | 20 | return ans; 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | int n,i; 27 | cin>>n; 28 | int ans; 29 | ans=split_perfect_squares(n); 30 | cout<<"\n"< 2 | #include 3 | using namespace std; 4 | 5 | #define err 0.000001 6 | 7 | double SquareRoot(int n) 8 | { 9 | if(n==1) 10 | return 1.0; 11 | 12 | double l,h,mid; 13 | double ans=n/2; 14 | double a1,a2; 15 | a1=1; 16 | a2=n; 17 | 18 | while(abs(a1-a2)>err) 19 | { 20 | a1=(a1+a2)/2; 21 | a2=(n/a1); 22 | } 23 | 24 | return a1; 25 | 26 | } 27 | 28 | int main() 29 | { 30 | int n; 31 | cout<<"Enter the num : "; 32 | cin>>n; 33 | cout<<"\nSquare root of "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | string s; 13 | cin>>s; 14 | 15 | int l = s.length(); 16 | int a=0,b=0,c=0; 17 | 18 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | bool lucky_num(int n) 6 | { 7 | static int counter=2; 8 | if(counter>n) 9 | return true; 10 | else if(counter%2==0) 11 | return false; 12 | 13 | int np=n; 14 | np=np-np/counter; 15 | counter++; 16 | 17 | return lucky_num(np); 18 | } 19 | 20 | int main() 21 | { 22 | int n; 23 | cout<<"Enter the num : "; 24 | cin>>n; 25 | cout<<"\n"; 26 | if(lucky_num(n)) 27 | { 28 | cout<<"its lucky number\n"; 29 | } 30 | else 31 | { 32 | cout<<"its not lucky num\n"; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Arrays/House_robber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int rob(vector &nums) 9 | { 10 | 11 | if (nums.size() == 1) 12 | return nums[0]; 13 | 14 | if (nums.size() == 0) 15 | return 0; 16 | 17 | int sum1 = nums[0], sum2 = max(nums[1], nums[0]); 18 | 19 | for (int i = 2; i < nums.size(); i++) 20 | { 21 | int op1 = nums[i] + sum1; 22 | int op2 = sum2; 23 | 24 | sum1 = sum2; 25 | sum2 = max(op1, op2); 26 | } 27 | 28 | return sum2; 29 | } 30 | }; -------------------------------------------------------------------------------- /Bit-Manipulation/game_Of_XOR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int game0fX0R(int N,int A[]) 5 | { 6 | if(N%2==0) 7 | { 8 | int sum=0; 9 | return sum; 10 | } 11 | else 12 | { 13 | int i; 14 | int sum=0; 15 | for(i=0;i>n; 26 | int a[n]; 27 | for(i=0;i>a[i]; 30 | } 31 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | void merge(long long arr1[], long long arr2[], int n, int m) 8 | { 9 | // code here 10 | int i, j; 11 | i = n - 1; 12 | j = 0; 13 | 14 | while (i >= 0 && j < m) 15 | { 16 | if (arr1[i] > arr2[j]) 17 | { 18 | int t = arr1[i]; 19 | arr1[i] = arr2[j]; 20 | arr2[j] = t; 21 | } 22 | 23 | i--; 24 | j++; 25 | } 26 | 27 | sort(arr1, arr1 + n); 28 | sort(arr2, arr2 + m); 29 | } 30 | 31 | int main() 32 | { 33 | } -------------------------------------------------------------------------------- /Strings/LPS_of_str.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void FindLPS(string s) 6 | { 7 | cout << s << "\n\n"; 8 | 9 | int n = s.length(); 10 | 11 | vector lps(n, 0); 12 | 13 | int j; 14 | for (int i = 1; i < n; i++) 15 | { 16 | j = lps[i - 1]; 17 | 18 | while (j > 0 && s[i] != s[j]) 19 | j = lps[j - 1]; 20 | 21 | if (s[i] == s[j]) 22 | j++; 23 | 24 | lps[i] = j; 25 | } 26 | 27 | for(int i = 0 ; i < n ; i++) 28 | cout<> s; 34 | 35 | FindLPS(s); 36 | } -------------------------------------------------------------------------------- /Algorithms/Euler_totient_fun.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | const int N=1e5+2, MOD=1e9+7; 5 | 6 | int totient[N]; 7 | 8 | int main() 9 | { 10 | int i,j; 11 | for(i=0;i "< 2 | 3 | #define INT_BITS 16 4 | using namespace std; 5 | int leftrotate(int n,int d) 6 | { 7 | int l,r; 8 | l=n<>(INT_BITS-d); 10 | 11 | return (l|r); 12 | } 13 | 14 | 15 | int rightrotate( int n, int d) 16 | { 17 | int l,r; 18 | l=n>>d; 19 | 20 | r=n<<(INT_BITS-d); 21 | 22 | return (l|r); 23 | 24 | } 25 | int main() { 26 | int t; 27 | cin>>t; 28 | while(t--) 29 | { 30 | int n,d; 31 | cin>>n>>d; 32 | 33 | cout< 2 | 3 | using namespace std; 4 | long long BitwiseAndPairSum(vector &A, int n) 5 | { 6 | long long sum = 0; 7 | 8 | for (int i = 31; i >= 0; i--) 9 | { 10 | int k = 0; 11 | 12 | for (int j = 0; j < n; j++) 13 | if (A[j] & (1 << i)) 14 | k++; 15 | 16 | sum += ((1 << i) * (k * (k - 1)) / 2); 17 | } 18 | 19 | return sum; 20 | } 21 | 22 | 23 | int main() 24 | { 25 | int n; 26 | cin >> n; 27 | 28 | vector A(n); 29 | 30 | for (int i = 0; i < n; i++) 31 | cin >> A[i]; 32 | 33 | cout << BitwiseAndPairSum(A, n); 34 | } -------------------------------------------------------------------------------- /Back-Tracking and Recursion/Sub_setSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | void func(int ind, int sum, vector &arr, int N, vector &sumSubset) 7 | { 8 | if (ind == N) 9 | { 10 | sumSubset.push_back(sum); 11 | return; 12 | } 13 | 14 | // pick the element 15 | func(ind + 1, sum + arr[ind], arr, N, sumSubset); 16 | 17 | // Do-not pick the element 18 | func(ind + 1, sum, arr, N, sumSubset); 19 | } 20 | 21 | 22 | vector subsetSums(vector arr, int N) 23 | { 24 | vector sumSubset; 25 | func(0, 0, arr, N, sumSubset); 26 | sort(sumSubset.begin(), sumSubset.end()); 27 | return sumSubset; 28 | } -------------------------------------------------------------------------------- /Dynamic-Programming/Catlan_nums_Dp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int Catlan(int n) 6 | { 7 | int i,j; 8 | 9 | if(n==0) 10 | return 1; 11 | 12 | if(n==1) 13 | return 1; 14 | 15 | int cat[n+1]; 16 | 17 | cat[0]=1; 18 | cat[1]=1; 19 | 20 | for(i=2;i<=n;i++) 21 | { 22 | cat[i]=0; 23 | for(j=0;j "<>n; 39 | 40 | cout< 2 | 3 | using namespace std; 4 | 5 | int dp[100][100]; 6 | 7 | 8 | int solve(int a[], int i, int j) 9 | { 10 | if(i==j) 11 | return dp[i][i]=a[i]; 12 | 13 | if(i>j) 14 | return 0; 15 | 16 | if(dp[i][j]!=-1) 17 | return dp[i][j]; 18 | 19 | 20 | int l=a[i]+min(solve(a,i+2,j),solve(a,i+1,j-1)); 21 | int r=a[j]+min(solve(a,i+1,j-1),solve(a,i,j-2)); 22 | 23 | return dp[i][j]=max(l,r); 24 | } 25 | 26 | int main() 27 | { 28 | memset(dp,-1,sizeof(dp)); 29 | int n,i,j; 30 | cout<<"Enter the num of coins : "; 31 | cin>>n; 32 | int a[n]; 33 | 34 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *next; 11 | 12 | Node(int d=0) 13 | { 14 | data=d; 15 | next=NULL; 16 | } 17 | }; 18 | 19 | 20 | Node *compute(Node *head) 21 | { 22 | // your code goes here 23 | if (head == NULL || head->next == NULL) 24 | return head; 25 | 26 | Node *curr = compute(head->next); 27 | 28 | if (head->data < curr->data) 29 | { 30 | head->data = curr->data; 31 | head->next = curr->next; 32 | } 33 | 34 | return head; 35 | } 36 | 37 | int main() 38 | { 39 | } -------------------------------------------------------------------------------- /Arrays/Longest_subarray_with_sum_zero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int maxLen(int A[], int n) 6 | { 7 | // Your code here 8 | unordered_map mpp; 9 | int maxi = 0; 10 | int sum = 0; 11 | for (int i = 0; i < n; i++) 12 | { 13 | sum += A[i]; 14 | if (sum == 0) 15 | { 16 | maxi = i + 1; 17 | } 18 | else 19 | { 20 | if (mpp.find(sum) != mpp.end()) 21 | { 22 | maxi = max(maxi, i - mpp[sum]); 23 | } 24 | else 25 | { 26 | mpp[sum] = i; 27 | } 28 | } 29 | } 30 | return maxi; 31 | } -------------------------------------------------------------------------------- /Algorithms/Hexadecimal_to_decimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int hexadecimal_to_decimal(string n) 6 | { 7 | int s=n.length(); 8 | int ans=0; 9 | int x=1; 10 | for(int i=s-1;i>=0;i--) 11 | { 12 | if(n[i]>='0' && n[i]<='9') 13 | { 14 | ans=ans+x*(n[i]-'0'); 15 | } 16 | else if(n[i]>='A' && n[i]<='F') 17 | { 18 | ans=ans+x*(n[i]-'A'+10); 19 | } 20 | 21 | x=x*16; 22 | } 23 | 24 | return ans; 25 | 26 | } 27 | int main() 28 | { 29 | string n; 30 | cin>>n; 31 | cout< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | double myPow(double x, int n) 9 | { 10 | 11 | double ans = 1.0; 12 | long long nn = n; 13 | if (nn < 0) 14 | nn = -1 * nn; 15 | while (nn) 16 | { 17 | if (nn % 2) 18 | { 19 | ans = ans * x; 20 | nn = nn - 1; 21 | } 22 | else 23 | { 24 | x = x * x; 25 | nn = nn / 2; 26 | } 27 | } 28 | if (n < 0) 29 | ans = (double)(1.0) / (double)(ans); 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /Arrays/first_non_repeating_char.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | char nonrepeatingCharacter(string S) 6 | { 7 | //Your code here 8 | int i,n; 9 | int hash[26]={0}; 10 | int l=S.length(); 11 | 12 | for(i=0;i>s; 34 | char c=nonrepeatingCharacter(s); 35 | cout<<"\n1st non repearing char is : "< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | void sortColors(vector &nums) 9 | { 10 | 11 | int low = 0, mid = 0, high = nums.size() - 1; 12 | 13 | while (mid <= high) 14 | { 15 | if (nums[mid] == 1) 16 | mid++; 17 | else if (nums[mid] == 0) 18 | { 19 | swap(nums[mid], nums[low]); 20 | low++; 21 | mid++; 22 | } 23 | else if (nums[mid] == 2) 24 | { 25 | swap(nums[mid], nums[high]); 26 | high--; 27 | } 28 | } 29 | } 30 | }; -------------------------------------------------------------------------------- /Bit-Manipulation/Hexadecimal_to_decimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int hexadecimal_to_decimal(string n) 6 | { 7 | int s=n.length(); 8 | int ans=0; 9 | int x=1; 10 | for(int i=s-1;i>=0;i--) 11 | { 12 | if(n[i]>='0' && n[i]<='9') 13 | { 14 | ans=ans+x*(n[i]-'0'); 15 | } 16 | else if(n[i]>='A' && n[i]<='F') 17 | { 18 | ans=ans+x*(n[i]-'A'+10); 19 | } 20 | 21 | x=x*16; 22 | } 23 | 24 | return ans; 25 | 26 | } 27 | int main() 28 | { 29 | string n; 30 | cin>>n; 31 | cout< 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | 11 | Node(int x = 0) 12 | { 13 | data = x; 14 | next = NULL; 15 | } 16 | }; 17 | 18 | int getMiddle(Node *head) 19 | { 20 | // Your code here 21 | Node *fast,*slow; 22 | fast=head; 23 | slow=head; 24 | 25 | while(fast!=NULL) 26 | { 27 | fast=fast->next; 28 | 29 | if(fast!=NULL) 30 | { 31 | fast=fast->next; 32 | slow=slow->next; 33 | } 34 | } 35 | 36 | return slow->data; 37 | } 38 | 39 | 40 | 41 | 42 | int main() 43 | { 44 | 45 | } -------------------------------------------------------------------------------- /Stacks and Queues/Reverse_Queue_recursively.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | void Reverse(queue&qu) 8 | { 9 | if(qu.empty()) 10 | return; 11 | 12 | int ele=qu.front(); 13 | qu.pop(); 14 | Reverse(qu); 15 | qu.push(ele); 16 | } 17 | int main() 18 | { 19 | int n,i,x; 20 | cout<<"Enter the num of elements in Qu : "; 21 | cin>>n; 22 | 23 | queuequ; 24 | 25 | for(i=0;i>x; 28 | qu.push(x); 29 | } 30 | 31 | Reverse(qu); 32 | 33 | cout<<"\n"; 34 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | string longestCommonPrefix(vector &strs) 9 | { 10 | 11 | if (strs.size() == 0) 12 | return ""; 13 | 14 | sort(strs.begin(), strs.end()); 15 | 16 | string a = strs[0]; 17 | string b = strs[strs.size() - 1]; 18 | 19 | string ans = ""; 20 | 21 | for (int i = 0; i < a.size(); i++) 22 | { 23 | if (a[i] == b[i]) 24 | { 25 | ans += a[i]; 26 | } 27 | else 28 | break; 29 | } 30 | 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Dynamic-Programming/Knapsack_0-N_DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n, i, j, W; 8 | 9 | cout << "Enter the Num of Elements : "; 10 | cin >> n; 11 | 12 | int val[n], wt[n]; 13 | 14 | cout << "Enter value and weight of n items : \n"; 15 | for (i = 0; i < n; i++) 16 | { 17 | cin >> val[i] >> wt[i]; 18 | } 19 | 20 | int dp[W + 1] = {0}; 21 | 22 | for (j = 0; j <= W; j++) 23 | { 24 | for (i = 0; i < n; i++) 25 | { 26 | if (j - wt[i] >= 0) 27 | { 28 | dp[j] = max(dp[j], val[i] + dp[j - wt[i]]); 29 | } 30 | } 31 | } 32 | 33 | cout << dp[W]; 34 | } -------------------------------------------------------------------------------- /Bit-Manipulation/sum_of_bit_difference_of_all_pairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long int sum_bit_diff(long int a[],long int n) 5 | { 6 | long long int i,j,count,ans; 7 | count=0; 8 | ans=0; 9 | for(i=0;i<32;i++) 10 | { 11 | count=0; 12 | for(j=0;j>n; 28 | long int a[n]; 29 | 30 | for(i=0;i>a[i]; 33 | } 34 | 35 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | cout<<"Enter the num of elements\n"; 6 | int n,i,q; 7 | cin>>n; 8 | cout<<"Enter the n elements\n"; 9 | int a[n]; 10 | for(i=0;i>a[i]; 13 | } 14 | int LR[2][q]; 15 | for(i=0;i>LR[0][i]>>LR[1][i]; 18 | } 19 | 20 | int xoor[n]; 21 | xoor[0]=a[0]; 22 | for(i=1;i 2 | using namespace std; 3 | 4 | int main() { 5 | int n,i; 6 | cin>>n; 7 | int price[n]; 8 | int buy,sell; 9 | for(i=0;i>price[i]; 12 | } 13 | i=0; 14 | while(i 2 | 3 | using namespace std; 4 | 5 | long long countTriplets(long long arr[], int n, long long sum) 6 | { 7 | // Your code goes here 8 | sort(arr, arr + n); 9 | int i, j, k; 10 | long long int ans = 0; 11 | for (i = 0; i < n - 2; i++) 12 | { 13 | j = i + 1; 14 | k = n - 1; 15 | 16 | while (j < k) 17 | { 18 | if (arr[i] + arr[j] + arr[k] >= sum) 19 | k--; 20 | else 21 | { 22 | ans = ans + (k - j); 23 | j++; 24 | } 25 | } 26 | } 27 | 28 | return ans; 29 | } 30 | 31 | int main() 32 | { 33 | 34 | } -------------------------------------------------------------------------------- /Stacks and Queues/LIS_in_NlogN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int LIS_size(vector&A) 7 | { 8 | vectorsi; 9 | 10 | for(int i = 0 ; i < A.size() ; i++) 11 | { 12 | if(si.empty() || A[i] > si.back()) 13 | si.push_back(A[i]); 14 | else 15 | { 16 | int idx = lower_bound(si.begin(), si.end(), A[i]) - si.begin(); 17 | si[idx] = A[i]; 18 | } 19 | 20 | } 21 | 22 | return si.size(); 23 | } 24 | 25 | 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | 31 | vectorA(n); 32 | 33 | for(int i = 0 ; i < n ; i ++) 34 | cin>>A[i]; 35 | 36 | int si = LIS_size(A); 37 | 38 | cout<<"\n"< 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | using namespace std; 5 | int main() 6 | { 7 | int n,i,j; 8 | cin>>n; 9 | int a[n+1]={0}; 10 | 11 | for(i=2;i<=n;i++) 12 | { 13 | if(a[i]==0) 14 | { 15 | for(j=i*i;j<=n;j=j+i) 16 | a[j]=1; 17 | } 18 | } 19 | 20 | int spf[n+1]; //smallest prime factor 21 | 22 | for(i=1;i<=n;i++) 23 | spf[i]=i; 24 | 25 | for(i=2;i<=n;i++) 26 | { 27 | if(spf[i]==i) 28 | { 29 | for(j=i*i;j<=n;j=j+i) 30 | { 31 | if(spf[j]==j) 32 | spf[j]=i; 33 | } 34 | } 35 | } 36 | 37 | for(i=2;i<=n;i++) 38 | { 39 | if(a[i]==0) 40 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | //code 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int n,i; 12 | cin>>n; 13 | int a[n],b[n]; 14 | 15 | for(i=0;i>a[i]; 18 | } 19 | for(i=0;i>b[i]; 22 | } 23 | 24 | sort(a,a+n); 25 | sort(b,b+n); 26 | int f=1; 27 | for(i=0;ib[i]) 30 | { 31 | f=0; 32 | break; 33 | } 34 | } 35 | 36 | if(f==1) 37 | cout<<"YES\n"; 38 | else 39 | cout<<"NO\n"; 40 | } 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Back-Tracking and Recursion/permutations_of_string(effective swapping).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | using namespace std; 5 | void permutate(char s[],int l,int h) 6 | { 7 | int i; 8 | if(l==h) 9 | { 10 | cout<>s; 34 | permutate(s,0,strlen(s)-1); 35 | } 36 | -------------------------------------------------------------------------------- /Algorithms/sieve_of_erathosthenes_SPF.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | using namespace std; 5 | int main() 6 | { 7 | int n,i,j; 8 | cin>>n; 9 | int a[n+1]={0}; 10 | 11 | for(i=2;i<=n;i++) 12 | { 13 | if(a[i]==0) 14 | { 15 | for(j=i*i;j<=n;j=j+i) 16 | a[j]=1; 17 | } 18 | } 19 | 20 | int spf[n+1]; //smallest prime factor 21 | 22 | for(i=1;i<=n;i++) 23 | spf[i]=i; 24 | 25 | for(i=2;i<=n;i++) 26 | { 27 | if(spf[i]==i) 28 | { 29 | for(j=i*i;j<=n;j=j+i) 30 | { 31 | if(spf[j]==j) 32 | spf[j]=i; 33 | } 34 | } 35 | } 36 | 37 | for(i=2;i<=n;i++) 38 | { 39 | if(a[i]==0) 40 | cout< 2 | #include 3 | using namespace std; 4 | 5 | bool mycomp(pairp1,pairp2) 6 | { 7 | return p1.first>p2.first; 8 | } 9 | 10 | int main() { 11 | int n,i; 12 | cin>>n; 13 | int cost[2*n][2]; 14 | 15 | vector< pair >v; 16 | 17 | for(i=0;i<2*n;i++) 18 | { 19 | cin>>cost[i][0]>>cost[i][1]; 20 | 21 | pairp; 22 | p.first=cost[i][1]-cost[i][0]; 23 | p.second=i; 24 | v.push_back(p); 25 | } 26 | 27 | sort(v.begin(),v.end(),mycomp); 28 | int sum=0; 29 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *left; 10 | Node *right; 11 | Node *next; 12 | 13 | Node(int d = 0) 14 | { 15 | data = d; 16 | left = NULL; 17 | right = NULL; 18 | next = NULL; 19 | } 20 | }; 21 | 22 | void populate(Node *root, Node **p) 23 | { 24 | if (root) 25 | { 26 | populate(root->right, p); 27 | 28 | root->next = (*p); 29 | (*p) = root; 30 | 31 | populate(root->left, p); 32 | } 33 | } 34 | void populateNext(Node *root) 35 | { 36 | //code here 37 | Node *p = NULL; 38 | populate(root, &p); 39 | } 40 | 41 | int main() 42 | { 43 | } -------------------------------------------------------------------------------- /Strings/col_base_operation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | Given a positive integer, return its corresponding column title as appear in an Excel sheet. 6 | Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, 7 | column 1 is named as “A”, column 2 as “B”, column 27 as “AA” and so on 8 | */ 9 | string colName(long long int n) 10 | { 11 | // your code here 12 | string ans = ""; 13 | 14 | while (n) 15 | { 16 | char ch = 'A' + (n - 1) % 26; // A is not mapped to 0 but it is mapped to 1 according to the question 17 | ans += ch; 18 | n = (n - 1) / 26; 19 | } 20 | 21 | reverse(ans.begin(), ans.end()); 22 | return ans; 23 | } 24 | 25 | int main() 26 | { 27 | } -------------------------------------------------------------------------------- /Arrays/printchangetut_2.6.2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | using namespace std; 5 | 6 | void printchange(int n) 7 | { 8 | int i; 9 | if(n<3 || n==4 || n==7) 10 | { 11 | cout<<"-1\n"; 12 | } 13 | else if(n%5==0) 14 | { 15 | if(n%10==0) 16 | { 17 | for(i=1;i>n; 44 | int count=0; 45 | printchange(n); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Algorithms/printchangetut_2.6.2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | using namespace std; 5 | 6 | void printchange(int n) 7 | { 8 | int i; 9 | if(n<3 || n==4 || n==7) 10 | { 11 | cout<<"-1\n"; 12 | } 13 | else if(n%5==0) 14 | { 15 | if(n%10==0) 16 | { 17 | for(i=1;i>n; 44 | int count=0; 45 | printchange(n); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Arrays/small_rope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,i; 11 | cin>>n; 12 | int a[n]; 13 | for(i=0;i>a[i]; 16 | } 17 | sort(a,a+n); 18 | i=0; 19 | int count=1; 20 | int left=n; 21 | while(i 2 | 3 | using namespace std; 4 | 5 | void Prod_array(int a[], int n) 6 | { 7 | int ans[n]; 8 | int i; 9 | ans[0]=1; 10 | for(i=1;i=0;i--) 16 | { 17 | ans[i]=ans[i]*temp; 18 | 19 | temp=temp*a[i]; 20 | } 21 | 22 | cout<<"\n\n"; 23 | 24 | for(i=0;i>n; 35 | int a[n]; 36 | cout<<"\nEnter the Elements : \n"; 37 | 38 | for(i=0;i>a[i]; 40 | 41 | 42 | Prod_array(a,n); 43 | } -------------------------------------------------------------------------------- /Algorithms/small_rope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,i; 11 | cin>>n; 12 | int a[n]; 13 | for(i=0;i>a[i]; 16 | } 17 | sort(a,a+n); 18 | i=0; 19 | int count=1; 20 | int left=n; 21 | while(i 2 | #include 3 | using namespace std; 4 | 5 | int platforms(int arr[], int dep[], int n) 6 | { 7 | sort(arr,arr+n); 8 | sort(dep,dep+n); 9 | int i,j,needed,curr; 10 | needed=0; 11 | curr=0; 12 | 13 | i=0;j=0; 14 | while(idep[j]) 23 | { 24 | curr--; 25 | j++; 26 | } 27 | } 28 | 29 | return needed; 30 | } 31 | 32 | int main() { 33 | int n,i; 34 | cin>>n; 35 | int arr[n],dep[n]; 36 | 37 | for(i=0;i>arr[i]>>dep[i]; 40 | } 41 | 42 | int p=platforms(arr,dep,n); 43 | cout< 2 | 3 | using namespace std; 4 | 5 | long long BitwiseORPairSum(vector &A, int n) 6 | { 7 | long long sum = 0; 8 | 9 | for (int i = 31; i >= 0; i--) 10 | { 11 | long long k1 = 0, k2 = 0; 12 | 13 | for (int j = 0; j < n; j++) 14 | { 15 | if (A[j] & (1 << i)) 16 | k1++; 17 | else 18 | k2++; 19 | 20 | sum += ((1 << i) * k1 * k2 + ((1 << i) * (k1) * (k1 - 1)) / 2); 21 | } 22 | } 23 | 24 | return sum; 25 | } 26 | 27 | int main() 28 | { 29 | int n; 30 | cin >> n; 31 | 32 | vector A(n); 33 | 34 | for (int i = 0; i < n; i++) 35 | cin >> A[i]; 36 | 37 | cout << BitwiseORPairSum(A, n); 38 | } -------------------------------------------------------------------------------- /Dynamic-Programming/Ugly-nums_DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ull unsigned long long 3 | using namespace std; 4 | 5 | ull getNthUglyNo(int n) 6 | { 7 | // code here 8 | 9 | int i, j, k; 10 | int c1 = 0, c2 = 0, c3 = 0; 11 | 12 | ull dp[n + 1]; 13 | dp[0] = 1; 14 | 15 | for (i = 1; i < n; i++) 16 | { 17 | dp[i] = min(2 * dp[c1], min(3 * dp[c2], 5 * dp[c3])); 18 | 19 | if (dp[i] == 2 * dp[c1]) 20 | c1++; 21 | if (dp[i] == 3 * dp[c2]) 22 | c2++; 23 | if (dp[i] == 5 * dp[c3]) 24 | c3++; 25 | } 26 | 27 | return dp[n - 1]; 28 | } 29 | 30 | int main() 31 | { 32 | cout << "Enter the n for nth ugly num : "; 33 | int n; 34 | cin >> n; 35 | 36 | cout << "nth Ugly num is : " << getNthUglyNo(n); 37 | } -------------------------------------------------------------------------------- /Trees/Invert_binary_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | using namespace std; 5 | 6 | class BinaryTree 7 | { 8 | public: 9 | int value; 10 | BinaryTree *left; 11 | BinaryTree *right; 12 | 13 | BinaryTree(int value); 14 | void insert(vector values, int i = 0); 15 | void invertedInsert(vector values, int i = 0); 16 | }; 17 | 18 | void invertBinaryTree(BinaryTree *tree) 19 | { 20 | // Write your code here. 21 | 22 | if (tree == NULL) 23 | return; 24 | 25 | if (tree->left) 26 | invertBinaryTree(tree->left); 27 | 28 | if (tree->right) 29 | invertBinaryTree(tree->right); 30 | 31 | BinaryTree *t; 32 | t = tree->left; 33 | tree->left = tree->right; 34 | tree->right = t; 35 | } 36 | 37 | int main() 38 | { 39 | } -------------------------------------------------------------------------------- /Arrays/smallest_missing_positive_number_IMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Given an unsorted integer array nums, return the smallest missing positive integer. 6 | 7 | // You must implement an algorithm that runs in O(n) time and uses constant extra space. 8 | 9 | class Solution 10 | { 11 | public: 12 | int firstMissingPositive(vector &nums) 13 | { 14 | 15 | int n = nums.size(); 16 | for (int i = 0; i < n; i++) 17 | { 18 | while (nums[i] >= 1 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) 19 | swap(nums[i], nums[nums[i] - 1]); 20 | } 21 | 22 | for (int i = 0; i < n; i++) 23 | { 24 | if (nums[i] != i + 1) 25 | return i + 1; 26 | } 27 | 28 | return n + 1; 29 | } 30 | }; -------------------------------------------------------------------------------- /Greedy/maximize_array_sum_in_K_negations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | long long int maximizeSum(long long int a[], int n, int k) 10 | { 11 | // Your code goes here 12 | priority_queue, greater> pq; 13 | 14 | for (int i = 0; i < n; i++) 15 | { 16 | pq.push(a[i]); 17 | } 18 | 19 | while (k--) 20 | { 21 | long long t = pq.top(); 22 | pq.pop(); 23 | 24 | t = t * (-1); 25 | 26 | pq.push(t); 27 | } 28 | 29 | long long sum = 0; 30 | while (!pq.empty()) 31 | { 32 | sum += (pq.top()); 33 | 34 | pq.pop(); 35 | } 36 | 37 | return sum; 38 | } 39 | 40 | int main() 41 | { 42 | } -------------------------------------------------------------------------------- /Searching and sorting/Insertion_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | void InsertionSort(int a[], int n) 8 | { 9 | int i,j,key; 10 | 11 | for(i=1;i=0 && a[j]>key) 17 | { 18 | a[j+1]=a[j]; 19 | j--; 20 | } 21 | 22 | a[j+1]=key; 23 | } 24 | } 25 | 26 | 27 | int main() 28 | { 29 | int n,i,j; 30 | cout<<"Enter the num of elements : "; 31 | cin>>n; 32 | int a[n]; 33 | cout<<"\nEnter the Elements of Array : \n"; 34 | 35 | for(i=0;i>a[i]; 38 | } 39 | 40 | InsertionSort(a,n); 41 | cout<<"\n\n"; 42 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | long int n,i,j,m; 13 | cin>>n; 14 | long long int a[n]; 15 | for(i=0;i>a[i]; 18 | } 19 | cin>>m; 20 | sort(a,a+n); 21 | i=0; 22 | j=m-1; 23 | long long int min_diff=INT_MAX; 24 | long long int diff; 25 | while(j 2 | 3 | using namespace std; 4 | vector nextLargerElement(vector arr, int n) 5 | { 6 | 7 | vector ans(arr.size()); 8 | // Your code here 9 | stack st; 10 | for (int i = 0; i < arr.size(); i++) 11 | { 12 | if (st.empty()) 13 | st.push(i); 14 | else 15 | { 16 | while (!st.empty() && arr[st.top()] < arr[i]) 17 | { 18 | ans[st.top()] = arr[i]; 19 | st.pop(); 20 | } 21 | 22 | st.push(i); 23 | } 24 | } 25 | 26 | while (!st.empty()) 27 | { 28 | ans[st.top()] = -1; 29 | st.pop(); 30 | } 31 | 32 | return ans; 33 | } 34 | 35 | 36 | int main() 37 | { 38 | 39 | } -------------------------------------------------------------------------------- /Algorithms/min_swaps_to_bring_k_togerther.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,i,j,k,count=0; 11 | cin>>n; 12 | int a[n]; 13 | for(i=0;i>a[i]; 16 | } 17 | cin>>k; 18 | 19 | for(i=0;ik) 32 | swap++; 33 | 34 | s++; 35 | } 36 | 37 | ans=min(ans,swap); 38 | } 39 | 40 | cout< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | vector getAverages(vector &nums, int k) 9 | { 10 | 11 | long long int n = nums.size(); 12 | vector ans(n, -1); 13 | 14 | long long int sum = 0; 15 | long int len = 2 * k + 1; 16 | 17 | if (n < len) 18 | return ans; 19 | 20 | for (long long int i = 0; i < len; i++) 21 | sum += nums[i]; 22 | 23 | long long int i = k; 24 | for (i = k; i + k + 1 < n; i++) 25 | { 26 | ans[i] = (sum / (2 * k + 1)); 27 | 28 | sum = sum - nums[i - k]; 29 | sum = sum + nums[i + k + 1]; 30 | } 31 | 32 | ans[i] = (sum / (2 * k + 1)); 33 | return ans; 34 | } 35 | }; -------------------------------------------------------------------------------- /Arrays/min_swaps_to_bring_k_togerther.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,i,j,k,count=0; 11 | cin>>n; 12 | int a[n]; 13 | for(i=0;i>a[i]; 16 | } 17 | cin>>k; 18 | 19 | for(i=0;ik) 32 | swap++; 33 | 34 | s++; 35 | } 36 | 37 | ans=min(ans,swap); 38 | } 39 | 40 | cout< 5 | using namespace std; 6 | 7 | void printTwoElements(int arr[], int size) 8 | { 9 | int i; 10 | cout << " The repeating element is "; 11 | 12 | for (i = 0; i < size; i++) { 13 | if (arr[abs(arr[i]) - 1] > 0) 14 | arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1]; 15 | else 16 | cout << abs(arr[i]) << "\n"; 17 | } 18 | 19 | cout << "and the missing element is "; 20 | for (i = 0; i < size; i++) { 21 | if (arr[i] > 0) 22 | cout << (i + 1); 23 | } 24 | } 25 | 26 | /* Driver code */ 27 | int main() 28 | { 29 | int arr[] = { 7, 3, 4, 5, 5, 6, 2 }; 30 | int n = sizeof(arr) / sizeof(arr[0]); 31 | printTwoElements(arr, n); 32 | } 33 | 34 | // This code is contributed by Shivi_Aggarwal 35 | -------------------------------------------------------------------------------- /Searching and sorting/Selection_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | void SelectionSort(int a[], int n) 8 | { 9 | int i,j,m,temp; 10 | 11 | for(i=0;i>n; 32 | int a[n]; 33 | cout<<"\nEnter the Elements of Array : \n"; 34 | 35 | for(i=0;i>a[i]; 38 | } 39 | 40 | SelectionSort(a,n); 41 | cout<<"\n\n"; 42 | for(i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int findMaxLen(string s) 8 | { 9 | // code here 10 | int i, ans = 0; 11 | stack st; 12 | st.push(-1); 13 | for (i = 0; i < s.length(); i++) 14 | { 15 | if (s[i] == '(') 16 | st.push(i); 17 | else 18 | { 19 | if (!st.empty()) 20 | st.pop(); 21 | 22 | if (!st.empty()) 23 | { 24 | ans = max(ans, i - st.top()); 25 | } 26 | 27 | if (st.empty()) 28 | st.push(i); 29 | } 30 | } 31 | 32 | return ans; 33 | } 34 | int main() 35 | { 36 | string s; 37 | cin>>s; 38 | int l=findMaxLen(s); 39 | 40 | cout<<"\n"< 2 | 3 | using namespace std; 4 | 5 | class BinaryTree 6 | { 7 | public: 8 | int value; 9 | BinaryTree *left; 10 | BinaryTree *right; 11 | 12 | BinaryTree(int value) 13 | { 14 | this->value = value; 15 | left = nullptr; 16 | right = nullptr; 17 | } 18 | }; 19 | 20 | int sum = 0; 21 | 22 | void FindDepth(BinaryTree *root, int d) 23 | { 24 | if (root == NULL) 25 | return; 26 | 27 | sum = sum + d; 28 | 29 | if (root->left) 30 | FindDepth(root->left, d + 1); 31 | 32 | if (root->right) 33 | FindDepth(root->right, d + 1); 34 | } 35 | int nodeDepths(BinaryTree *root) 36 | { 37 | // Write your code here. 38 | sum = 0; 39 | 40 | FindDepth(root, 0); 41 | return sum; 42 | } 43 | 44 | int main() 45 | { 46 | } -------------------------------------------------------------------------------- /Arrays/Longest_consecutive_sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int longestConsecutive(vector &nums) 9 | { 10 | 11 | unordered_set st; 12 | 13 | for (auto it : nums) 14 | st.insert(it); 15 | 16 | int count = 0; 17 | int curr; 18 | for (auto n : nums) 19 | { 20 | if (st.find(n - 1) != st.end()) 21 | continue; 22 | else 23 | { 24 | curr = 0; 25 | 26 | while (st.find(n) != st.end()) 27 | { 28 | curr++; 29 | n++; 30 | } 31 | 32 | count = max(count, curr); 33 | } 34 | } 35 | 36 | return count; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /Greedy/smallest_string_with_given_numeric_value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | string getSmallestString(int n, int k) 9 | { 10 | 11 | string ans = ""; 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | ans += "a"; 16 | } 17 | 18 | k -= n; 19 | 20 | for (int i = n - 1; i >= 0; i--) 21 | { 22 | if (k == 0) 23 | break; 24 | 25 | if (k < 25) 26 | { 27 | ans[i] = (char)('a' + k); 28 | k == 0; 29 | break; 30 | } 31 | else 32 | { 33 | ans[i] = (char)('a' + 25); 34 | k = k - 25; 35 | } 36 | } 37 | 38 | return ans; 39 | } 40 | }; -------------------------------------------------------------------------------- /Searching and sorting/DnF_sort(0,1,2).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | using namespace std; 5 | int main() { 6 | int n,i,j; 7 | cout<<"Enter the num of elements in array\n"; 8 | cin>>n; 9 | int a[n]; 10 | cout<<"Enter the elements 0,1,2\n"; 11 | for(i=0;i>a[i]; 14 | } 15 | int l,h,m; 16 | l=0; 17 | m=0; 18 | h=n-1; 19 | while(m 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | 11 | Node(int d = 0) 12 | { 13 | data = d; 14 | next = NULL; 15 | } 16 | }; 17 | 18 | long long multiplyTwoLists(Node *first, Node *second) 19 | { 20 | long long N = 1000000007; 21 | long long num1 = 0, num2 = 0; 22 | while (first || second) 23 | { 24 | 25 | if (first) 26 | { 27 | num1 = ((num1)*10) % N + first->data; 28 | first = first->next; 29 | } 30 | 31 | if (second) 32 | { 33 | num2 = ((num2)*10) % N + second->data; 34 | second = second->next; 35 | } 36 | } 37 | return ((num1 % N) * (num2 % N)) % N; 38 | } 39 | 40 | int main() 41 | { 42 | } -------------------------------------------------------------------------------- /Graphs/Minimum_steps_to_reach_destination.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | /* 12 | int steps(int src, int step, int D) 13 | { 14 | if(abs(src)>D) 15 | return INT_MAX; 16 | 17 | if(src==D) 18 | return step; 19 | 20 | int pos=steps(src+step+1, step+1,D); 21 | int neg=steps(src-step-1, step+1,D); 22 | 23 | return min(pos,neg); 24 | } 25 | */ 26 | int minSteps(int D) 27 | { 28 | // code here 29 | // return steps(0,0,D); 30 | 31 | int sum = 0, step = 0; 32 | 33 | while (sum < D || (sum - D) % 2 != 0) 34 | { 35 | step++; 36 | sum += step; 37 | } 38 | 39 | return step; 40 | } -------------------------------------------------------------------------------- /Binary Search Trees/Find_closests_val_in_BST_to_target.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | class BST { 10 | public: 11 | int value; 12 | BST *left; 13 | BST *right; 14 | 15 | BST(int val); 16 | BST &insert(int val); 17 | }; 18 | 19 | int findClosestValueInBst(BST *tree, int target) { 20 | // Write your code here. 21 | int val=tree->value; 22 | 23 | 24 | int d=abs(tree->value-target); 25 | 26 | while(tree) 27 | { 28 | int diff=abs(tree->value-target); 29 | if(diffvalue; 33 | } 34 | 35 | 36 | 37 | if(tree->value > target) 38 | { 39 | tree=tree->left; 40 | } 41 | else 42 | { 43 | tree=tree->right; 44 | } 45 | } 46 | 47 | return val; 48 | } 49 | 50 | int main() 51 | { 52 | 53 | } -------------------------------------------------------------------------------- /solving/ticks.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | 11 | while(t--) 12 | { 13 | int n,m,k; 14 | int i,j; 15 | cin>>n>>m>>k; 16 | 17 | vector>grid(n,vector(m)); 18 | 19 | for(i=0;i>grid[i][j]; 24 | } 25 | } 26 | 27 | for(i=n-1;i>=0;i--) 28 | { 29 | for(j=0;j 2 | 3 | using namespace std; 4 | 5 | int vis[100001]; 6 | /* 7 | Given a Graph of V vertices and E edges and another edge(c - d), the task is to find if the given edge is a Bridge. i.e., 8 | removing the edge disconnects the graph. 9 | */ 10 | int dfs(int i, vector adj[], int c, int d) 11 | { 12 | 13 | vis[i] = 1; 14 | 15 | for (auto x : adj[i]) 16 | { 17 | if (i == c && x == d) 18 | continue; 19 | if (!vis[x]) 20 | dfs(x, adj, c, d); 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | int isBridge(int V, vector adj[], int c, int d) 27 | { 28 | // Code here 29 | for (int i = 0; i < V; i++) 30 | vis[i] = 0; 31 | 32 | dfs(c, adj, c, d); 33 | 34 | return (!vis[d]); 35 | } 36 | 37 | int main() 38 | { 39 | int u,v; 40 | cout<<"Enter"; 41 | } -------------------------------------------------------------------------------- /Greedy/Adding_missing_number_in_the_patch_IMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int minPatches(vector &nums, int n) 9 | { 10 | 11 | long misses = 1, added = 0; 12 | 13 | int i = 0; 14 | 15 | while (misses <= n) 16 | { 17 | if (i < nums.size() && nums[i] <= misses) 18 | { 19 | misses += nums[i]; 20 | i++; 21 | } 22 | else 23 | { 24 | misses += misses; 25 | added++; 26 | } 27 | } 28 | 29 | return added; 30 | } 31 | }; 32 | 33 | //https://leetcode.com/problems/patching-array/discuss/78492/C%2B%2B-8ms-greedy-solution-with-explanation 34 | 35 | //https://leetcode.com/problems/patching-array/discuss/78488/Solution-%2B-explanation -------------------------------------------------------------------------------- /Heaps/Laptop_rentenals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int laptopRentals(vector> times) { 7 | // Write your code here. 8 | int count=0; 9 | int m=0; 10 | 11 | sort(times.begin(),times.end()); 12 | priority_queue< pair , vector> , greater> > pq; 13 | 14 | int n=times.size(); 15 | 16 | for(int i=0;ip=pq.top(); 26 | if(times[i][0] < p.first) 27 | { 28 | count++; 29 | 30 | } 31 | else 32 | { 33 | pq.pop(); 34 | } 35 | 36 | pq.push({times[i][1],times[i][0]}); 37 | } 38 | } 39 | 40 | return count; 41 | 42 | } 43 | 44 | 45 | int main() 46 | { 47 | 48 | } -------------------------------------------------------------------------------- /Trees/Flatten_tree_into_LL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node* left; 10 | Node *right; 11 | 12 | Node(int d=0) 13 | { 14 | data=0; 15 | left=NULL; 16 | right=NULL; 17 | } 18 | }; 19 | 20 | void flatten(Node *root) 21 | { 22 | if(root==NULL) 23 | return; 24 | 25 | if(root->left==NULL && root->right==NULL) 26 | return ; 27 | 28 | if(root->left) 29 | { 30 | flatten(root->left); 31 | 32 | Node *temp=root->right; 33 | 34 | root->right=root->left; 35 | root->left=NULL; 36 | 37 | Node *t=root; 38 | 39 | while(t->right!=NULL) 40 | t=t->right; 41 | 42 | t->right=temp; 43 | } 44 | 45 | flatten(root->right); 46 | } 47 | 48 | int main() 49 | { 50 | 51 | } -------------------------------------------------------------------------------- /Arrays/reduce_array(PAIRS).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | bool mycmp(pairp1, pairp2) 8 | { 9 | return p1.first>n; 17 | int a[n]; 18 | for(i=0;i>a[i]; 21 | } 22 | 23 | vector< pair >v; 24 | for(i=0;ip; 27 | 28 | p.first=a[i]; 29 | p.second=i; 30 | v.push_back(p); 31 | 32 | //can also be done by inbuilt function 33 | // v.push_back(make_pair(a[i],i)); 34 | } 35 | 36 | 37 | sort(v.begin(),v.end(),mycmp); 38 | 39 | for(i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *left; 12 | Node *right; 13 | 14 | Node(int d = 0) 15 | { 16 | data = d; 17 | left = NULL; 18 | right = NULL; 19 | } 20 | }; 21 | 22 | 23 | 24 | int getCount(Node *root, int l, int h) 25 | { 26 | // your code goes here 27 | if(root==NULL) 28 | return 0; 29 | 30 | if(root->data==l && root->data==h) 31 | return 1; 32 | 33 | if(root->data>=l && root->data<=h) 34 | { 35 | return 1 + getCount(root->left,l,h) + getCount(root->right,l,h); 36 | } 37 | 38 | if(root->data < l) 39 | return getCount(root->right,l,h); 40 | else 41 | return getCount(root->left,l,h); 42 | } 43 | 44 | int main() 45 | { 46 | 47 | } -------------------------------------------------------------------------------- /Binary Search Trees/Kth_largest_in_BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *left; 12 | Node *right; 13 | 14 | Node(int d = 0) 15 | { 16 | data = d; 17 | left = NULL; 18 | right = NULL; 19 | } 20 | }; 21 | 22 | void Traverse(Node *r, int k, int &i, vector &ans) 23 | { 24 | if (i > k) 25 | return; 26 | 27 | if (r) 28 | { 29 | Traverse(r->right, k, i, ans); 30 | ans.push_back(r->data); 31 | i++; 32 | Traverse(r->left, k, i, ans); 33 | } 34 | } 35 | int kthLargest(Node *root, int K) 36 | { 37 | //Your code here 38 | vector ans; 39 | int i = 0; 40 | Traverse(root, K, i, ans); 41 | 42 | return ans[K - 1]; 43 | } 44 | 45 | int main() 46 | { 47 | } -------------------------------------------------------------------------------- /Greedy/min_buckets_water.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int minimumBuckets(string street) 9 | { 10 | 11 | int cnt = 0; 12 | int n = street.size(); 13 | 14 | for (int i = 0; i < n;) 15 | { 16 | if (street[i] == 'H') 17 | { 18 | if (i + 1 < n && street[i + 1] == '.') 19 | { 20 | cnt++; 21 | i += 3; 22 | continue; 23 | } 24 | else if (i - 1 >= 0 && street[i - 1] == '.') 25 | { 26 | cnt++; 27 | i++; 28 | continue; 29 | } 30 | 31 | return -1; 32 | } 33 | 34 | i++; 35 | } 36 | 37 | return cnt; 38 | } 39 | }; -------------------------------------------------------------------------------- /Divide and Conqure/Nth_root_of_number_m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | double multiply(double mid, int n) 6 | { 7 | double ans = 1.0; 8 | 9 | for(int i=1;i<=n;i++) 10 | ans *= mid; 11 | 12 | return ans; 13 | } 14 | 15 | double Nth_root(int m, int n) 16 | { 17 | double low = 1.0; 18 | double high = m; 19 | double eps = 1e-6; 20 | double mid; 21 | while(abs(high-low)>eps) 22 | { 23 | mid = (high+low)/2.0; 24 | 25 | if(multiply(mid,n) > m) 26 | { 27 | high = mid; 28 | } 29 | else 30 | { 31 | low = mid; 32 | } 33 | } 34 | 35 | return low; 36 | } 37 | int main() 38 | { 39 | int n,m; 40 | 41 | cout<<"Enter the number 'm' and root 'n' : "; 42 | cin>>m>>n; 43 | 44 | double r = Nth_root(m,n); 45 | 46 | cout<<"\n\n ANS : "< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | vector sortKSortedArray(vector array, int k) { 8 | // Write your code here. 9 | if(k==0) 10 | return array; 11 | priority_queue, greater >pq; 12 | 13 | for(int i=0;i>n>>k; 36 | vectorarray(n); 37 | 38 | for(i=0;i>array[i]; 40 | 41 | vectorans=sortKSortedArray(array,k); 42 | 43 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *left; 10 | Node *right; 11 | 12 | Node(int d=0) 13 | { 14 | data=d; 15 | left=NULL; 16 | right=NULL; 17 | } 18 | }; 19 | 20 | bool isBST(vector&pre) 21 | { 22 | int n,i,j; 23 | n=pre.size(); 24 | 25 | if(n==0 || n==1) 26 | return true; 27 | 28 | stackst; 29 | 30 | int root=INT_MIN; 31 | 32 | for(i=0;i 2 | 3 | using namespace std; 4 | 5 | // Complete the candies function below. 6 | int main() 7 | { 8 | long int n, i, j; 9 | cin >> n; 10 | long int a[n], left[n] = {0}, right[n] = {0}; 11 | 12 | for (i = 0; i < n; i++) 13 | { 14 | cin >> a[i]; 15 | } 16 | left[0] = 1; 17 | for (i = 1; i < n; i++) 18 | { 19 | if (a[i] > a[i - 1]) 20 | left[i] = 1 + left[i - 1]; 21 | else 22 | left[i] = 1; 23 | } 24 | 25 | right[n - 1] = 1; 26 | for (i = n - 2; i >= 0; i--) 27 | { 28 | if (a[i] > a[i + 1]) 29 | right[i] = 1 + right[i + 1]; 30 | else 31 | right[i] = 1; 32 | } 33 | 34 | long int sum = 0; 35 | for (i = 0; i < n; i++) 36 | { 37 | sum = sum + max(left[i], right[i]); 38 | } 39 | 40 | cout << sum; 41 | } -------------------------------------------------------------------------------- /Binary Search Trees/Convert_normalBST_to_balanced_BST.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *left; 11 | Node *right; 12 | Node(int x=0) 13 | { 14 | data=x; 15 | left = NULL; 16 | right = NULL; 17 | } 18 | }; 19 | 20 | void StoreInorder(Node *r, vector&v) 21 | { 22 | if(r==NULL) 23 | return; 24 | 25 | StoreInorder(r->left,v); 26 | v.push_back(r); 27 | StoreInorder(r->right,v); 28 | } 29 | 30 | Node* BuildBalancedBST(vector&v, int left, int right) 31 | { 32 | if(left>right) 33 | return NULL; 34 | 35 | 36 | int mid = (left + right)/2; 37 | Node *r = v[mid]; 38 | r->left = BuildBalancedBST(v,left,mid-1); 39 | r->right = BuildBalancedBST(v,mid+1,right); 40 | 41 | return r; 42 | } 43 | 44 | 45 | int main() 46 | { 47 | 48 | } -------------------------------------------------------------------------------- /Graphs/DisJoint_Set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | vectorparent(100000); 7 | 8 | 9 | vectorrank(100000); 10 | 11 | 12 | void makeSet(int n) 13 | { 14 | for(int i = 1; i <=n ; i++) 15 | parent[i] = i; 16 | 17 | for(int i = 1 ; i <= n ; i++) 18 | rank[i] = 0; 19 | } 20 | 21 | int findParent(int node) 22 | { 23 | if(node == parent[node]) 24 | return node; 25 | else 26 | return parent[node] = findParent(parent[node]); 27 | } 28 | 29 | 30 | void Union(int u , int v) 31 | { 32 | u = findParent(u); 33 | v = findParent(v); 34 | 35 | if(rank[u] < rank[v]) 36 | parent[u] = v; 37 | else if(rank[v] < rank[u]) 38 | parent[v] = u; 39 | else 40 | { 41 | parent[v] = u; 42 | rank[u]++; 43 | } 44 | } 45 | 46 | 47 | int main() 48 | { 49 | int n,e; 50 | 51 | cin>>n; 52 | 53 | 54 | } -------------------------------------------------------------------------------- /Heaps/conncect_n_ropes_optimal_pattern_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | long long minCost(long long arr[], long long n) 8 | { 9 | // Your code here 10 | if (n == 1) 11 | return 0; 12 | 13 | if (n == 2) 14 | return (arr[0] + arr[1]); 15 | 16 | priority_queue, greater> pq; 17 | 18 | for (auto i = 0; i < n; i++) 19 | { 20 | pq.push(arr[i]); 21 | } 22 | 23 | long long sum = 0; 24 | 25 | while (!pq.empty()) 26 | { 27 | long long a = pq.top(); 28 | pq.pop(); 29 | 30 | if (pq.empty()) 31 | return sum; 32 | 33 | long long b = pq.top(); 34 | pq.pop(); 35 | 36 | sum += (a + b); 37 | 38 | pq.push(a + b); 39 | } 40 | 41 | return sum; 42 | } 43 | 44 | int main() 45 | { 46 | } -------------------------------------------------------------------------------- /Dynamic-Programming/Binomial_CoefficientProblem_DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int nCk(int n, int k) 6 | { 7 | if(n>n; 42 | cout<<"Enter the val of K : "; 43 | cin>>k; 44 | 45 | cout<<"Val of nCk is "< 2 | using namespace std; 3 | 4 | int main() { 5 | //code 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n,i,j,k; 11 | cin>>n; 12 | int a[n]; 13 | for(i=0;i>a[i]; 16 | } 17 | 18 | int great[n]; 19 | int small[n]; 20 | great[0]=a[0]; 21 | small[n-1]=a[n-1]; 22 | k=0; 23 | for(i=1;i=0;i--) 28 | { 29 | small[i]=min(small[i+1],a[i]); 30 | } 31 | 32 | for(i=1;i 2 | 3 | using namespace std; 4 | 5 | 6 | /* 7 | We need to form all possible subsets, without duplicate subsets. 8 | 9 | */ 10 | class Solution 11 | { 12 | public: 13 | void Subsets(int ind, vector &nums, vector> &ans, vector &ds) 14 | { 15 | ans.push_back(ds); 16 | 17 | for (int i = ind; i != nums.size(); i++) 18 | { 19 | if (i != ind && nums[i] == nums[i - 1]) 20 | continue; 21 | 22 | ds.push_back(nums[i]); 23 | Subsets(i + 1, nums, ans, ds); 24 | ds.pop_back(); 25 | } 26 | } 27 | vector> subsetsWithDup(vector &nums) 28 | { 29 | 30 | vector> ans; 31 | vector ds; 32 | sort(nums.begin(), nums.end()); 33 | Subsets(0, nums, ans, ds); 34 | 35 | return ans; 36 | } 37 | }; -------------------------------------------------------------------------------- /Link-Lists/Nth_node_from_last.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next; 12 | 13 | Node(int d=0) 14 | { 15 | data=d; 16 | next=NULL; 17 | } 18 | }; 19 | 20 | int getNthFromLast(Node *head, int n) 21 | { 22 | // Your code here 23 | Node *p1,*p2; 24 | int i=0; 25 | 26 | p1=head; 27 | p2=head; 28 | 29 | while(inext; 32 | i++; 33 | } 34 | 35 | if(p2!=NULL) 36 | { 37 | while(p2) 38 | { 39 | p1=p1->next; 40 | p2=p2->next; 41 | } 42 | 43 | return p1->data; 44 | } 45 | if(p2==NULL) 46 | { 47 | if(i==n) 48 | return p1->data; 49 | else 50 | return -1; 51 | } 52 | } 53 | 54 | int main() 55 | { 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Arrays/Array_permutations_easy_way.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | Given an array nums of distinct integers, 6 | return all the possible permutations. You can return the answer in any order. 7 | */ 8 | class Solution 9 | { 10 | public: 11 | void PermuteRecursive(vector &nums, int begin, vector> &ans) 12 | { 13 | if (begin >= nums.size()) 14 | { 15 | ans.push_back(nums); 16 | return; 17 | } 18 | 19 | int n = nums.size(); 20 | for (int i = begin; i < n; i++) 21 | { 22 | swap(nums[begin], nums[i]); 23 | PermuteRecursive(nums, begin + 1, ans); 24 | swap(nums[begin], nums[i]); 25 | } 26 | } 27 | vector> permute(vector &nums) 28 | { 29 | 30 | vector> ans; 31 | 32 | PermuteRecursive(nums, 0, ans); 33 | 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /Dynamic-Programming/min_num_of_jumps(DYNAMIC).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int hopper(int a[],int n) 6 | { 7 | int *jump; 8 | int i,j; 9 | jump=new int [n]; 10 | jump[0]=0; 11 | if(n==0 || a[0]==0) 12 | { 13 | return INT_MAX; 14 | } 15 | 16 | for(i=1;i>n; 35 | cout<<"Enter theheight of towers\n"; 36 | int a[n]; 37 | for(i=0;i>a[i]; 40 | } 41 | 42 | int x=hopper(a,n); 43 | cout< 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | Node *bottom; 11 | 12 | Node(int x = 0) 13 | { 14 | data = x; 15 | next = NULL; 16 | bottom = NULL; 17 | } 18 | }; 19 | 20 | Node* faltten(Node *head) 21 | { 22 | if(head==NULL) 23 | return; 24 | 25 | Node *curr,*tail,*t; 26 | curr=head; 27 | tail=head; 28 | 29 | while(tail->next!=NULL) 30 | tail=tail->next; 31 | 32 | while(curr!=tail) 33 | { 34 | if(curr->bottom!=NULL) 35 | { 36 | tail->next=curr->bottom; 37 | 38 | t=curr->bottom; 39 | 40 | while(t->next!=NULL) 41 | t=t->next; 42 | 43 | tail=t; 44 | } 45 | 46 | curr=curr->next; 47 | } 48 | 49 | return head; 50 | } 51 | 52 | int main() 53 | { 54 | 55 | } -------------------------------------------------------------------------------- /Searching and sorting/Bubble_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | void BubbleSort(int a[], int n) 7 | { 8 | int i,j,flag=0,temp; 9 | 10 | for(i=0;ia[j+1]) 17 | { 18 | temp=a[j]; 19 | a[j]=a[j+1]; 20 | a[j+1]=temp; 21 | flag=0; 22 | } 23 | } 24 | 25 | if(flag==1) 26 | break; 27 | } 28 | } 29 | 30 | 31 | int main() 32 | { 33 | int n,i,j; 34 | cout<<"Enter the num of elements : "; 35 | cin>>n; 36 | int a[n]; 37 | cout<<"\nEnter the Elements of Array : \n"; 38 | 39 | for(i=0;i>a[i]; 42 | } 43 | 44 | BubbleSort(a,n); 45 | cout<<"\n\n"; 46 | for(i=0;i 2 | #define size 1000 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | 5 | using namespace std; 6 | long int trapped_water(int a[],int n) 7 | { 8 | int i,j; 9 | int left[n],right[n]; 10 | left[0]=a[0]; 11 | 12 | 13 | for(i=1;i=0;i--) 21 | { 22 | right[i]=max(a[i],right[i+1]); 23 | } 24 | long int water=0; 25 | for(i=0;i>n; 37 | int a[size]; 38 | cout<<"Enter the elements of array\n"; 39 | 40 | for(i=0;i>a[i]; 43 | } 44 | 45 | long int ans=trapped_water(a,n); 46 | 47 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *next; 11 | 12 | Node(int x=0) 13 | { 14 | data=x; 15 | next=NULL; 16 | } 17 | }; 18 | 19 | 20 | 21 | 22 | Node *removeDuplicates(Node *head) 23 | { 24 | // your code goes here 25 | Node *curr,*Next; 26 | curr=head; 27 | Next=curr->next; 28 | 29 | while(Next) 30 | { 31 | if(curr->data==Next->data) 32 | { 33 | curr->next=Next->next; 34 | Next->next=NULL; 35 | 36 | delete Next; 37 | 38 | Next=curr->next; 39 | } 40 | else 41 | { 42 | curr=curr->next; 43 | Next=Next->next; 44 | } 45 | } 46 | 47 | return head; 48 | 49 | } 50 | 51 | int main() 52 | { 53 | 54 | } -------------------------------------------------------------------------------- /Back-Tracking and Recursion/Generate_parantheses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void find(int n,int open,int close, string s, vector&ans) 7 | { 8 | if(open>n || close>n || close>open) 9 | return; 10 | if(s.length()==(2*n) && open==close) 11 | { 12 | ans.push_back(s); 13 | } 14 | 15 | find(n,open+1,close,s+"(",ans); 16 | find(n,open,close+1,s+")",ans); 17 | } 18 | 19 | vector Generate(int n) 20 | { 21 | vectorans; 22 | int open=0,close=0; 23 | string s=""; 24 | find(n,open,close,s,ans); 25 | 26 | return ans; 27 | } 28 | 29 | 30 | int main() 31 | { 32 | int n,i; 33 | cout<<"Enter Num of Paranthesis(num of pairs) : "; 34 | cin>>n; 35 | 36 | vectorans; 37 | ans=Generate(n); 38 | cout<<"\n********************\n\n"; 39 | for(i=0;i 2 | #include 3 | 4 | using namespace std; 5 | 6 | int Find_Missing(int a[], int n) 7 | { 8 | int i,j,d,A,N; 9 | A=a[0]; 10 | d=min(a[1]-a[0], a[2]-a[1]); 11 | 12 | int low,high,mid; 13 | low=0; 14 | high=n-1; 15 | 16 | while(low<=high) 17 | { 18 | mid=(low+high)/2; 19 | 20 | if(a[mid+1]-a[mid]!=d) 21 | return (a[mid]+d); 22 | 23 | N=mid+1; 24 | 25 | if(a[mid]==(A+(N-1)*d)) 26 | low=mid+1; 27 | else 28 | { 29 | high=mid-1; 30 | } 31 | } 32 | 33 | return -1; 34 | } 35 | 36 | int main() 37 | { 38 | int n,i,j; 39 | cout<<"Enter the num of terms : "; 40 | cin>>n; 41 | cout<<"\nEnter the n terms : \n"; 42 | int a[n]; 43 | for(i=0;i>a[i]; 46 | } 47 | 48 | cout<<"\nMissing term is "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int d = 0) 14 | { 15 | data = d; 16 | left = NULL; 17 | right = NULL; 18 | } 19 | }; 20 | 21 | void Traverse(Node *root, int level, int *maxlevel, vector &ans) 22 | { 23 | if (root) 24 | { 25 | if (level > *maxlevel) 26 | { 27 | ans.push_back(root->data); 28 | *maxlevel = level; 29 | } 30 | 31 | Traverse(root->left, level + 1, maxlevel, ans); 32 | Traverse(root->right, level + 1, maxlevel, ans); 33 | } 34 | } 35 | vector leftView(Node *root) 36 | { 37 | // Your Code here 38 | vector ans; 39 | 40 | int maxlevel = 0; 41 | 42 | Traverse(root, 1, &maxlevel, ans); 43 | 44 | return ans; 45 | } 46 | 47 | int main() 48 | { 49 | } -------------------------------------------------------------------------------- /Trees/Right_view_of_BT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Node 7 | { 8 | public: 9 | int data; 10 | Node *left; 11 | Node *right; 12 | 13 | Node(int d = 0) 14 | { 15 | data = d; 16 | left = NULL; 17 | right = NULL; 18 | } 19 | }; 20 | 21 | void Traverse(Node *root, int level, int *maxlevel, vector &ans) 22 | { 23 | if (root) 24 | { 25 | if (level > *maxlevel) 26 | { 27 | ans.push_back(root->data); 28 | *maxlevel = level; 29 | } 30 | 31 | Traverse(root->right, level + 1, maxlevel, ans); 32 | Traverse(root->left, level + 1, maxlevel, ans); 33 | } 34 | } 35 | vector rightView(Node *root) 36 | { 37 | // Your Code here 38 | vector ans; 39 | 40 | int maxlevel = 0; 41 | 42 | Traverse(root, 1, &maxlevel, ans); 43 | 44 | return ans; 45 | } 46 | 47 | int main() 48 | { 49 | } -------------------------------------------------------------------------------- /Dynamic-Programming/number_of_steps(efficent by dynmic programing).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | using namespace std; 5 | int steps(int n,int a[],int x) 6 | { 7 | if(n==0) 8 | return 1; 9 | else 10 | { 11 | int num_of_ways[n+1]; 12 | num_of_ways[0]=1; 13 | int i,j; 14 | for(i=1;i<=n;i++) 15 | { 16 | int total=0; 17 | for(j=0;j=0) 20 | total=total+num_of_ways[i-a[j]]; 21 | } 22 | num_of_ways[i]=total; 23 | } 24 | 25 | return num_of_ways[n]; 26 | } 27 | } 28 | int main() 29 | { 30 | int n,i; 31 | cout<<"Enter num of steps in staircase\n"; 32 | cin>>n; 33 | cout<<"Enter num of steps which are allowed\n"; 34 | int x; 35 | cin>>x; 36 | cout<<"Enter the value of steps\n"; 37 | int a[x]; 38 | for(i=0;i>a[i]; 41 | } 42 | int ans=steps(n,a,x); 43 | 44 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int celebrity(vector> &M, int n) 8 | { 9 | // code here 10 | stack st; 11 | int i; 12 | for (i = 0; i < n; i++) 13 | { 14 | st.push(i); 15 | } 16 | 17 | while (st.size() > 1) 18 | { 19 | int a = st.top(); 20 | st.pop(); 21 | int b = st.top(); 22 | st.pop(); 23 | 24 | if (M[a][b] == 1) 25 | { 26 | st.push(b); 27 | } 28 | else if (M[a][b] == 0) 29 | { 30 | st.push(a); 31 | } 32 | } 33 | 34 | if (st.size() == 0) 35 | return -1; 36 | 37 | int c = st.top(); 38 | 39 | for (i = 0; i < n; i++) 40 | { 41 | if (i != c && (M[c][i] == 1 || M[i][c] == 0)) 42 | return -1; 43 | } 44 | 45 | return c; 46 | } 47 | 48 | int main() 49 | { 50 | } -------------------------------------------------------------------------------- /Stacks and Queues/Merge_overlapping_intervals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector> merge(vector> &intervals) 7 | { 8 | 9 | sort(intervals.begin(), intervals.end()); 10 | 11 | stack> st; 12 | 13 | st.push(intervals[0]); 14 | 15 | for (int i = 1; i < intervals.size(); i++) 16 | { 17 | vector tp = st.top(); 18 | 19 | if (tp[1] < intervals[i][0]) 20 | st.push(intervals[i]); 21 | else 22 | { 23 | if (tp[1] < intervals[i][1]) 24 | { 25 | tp[1] = intervals[i][1]; 26 | } 27 | st.pop(); 28 | st.push(tp); 29 | } 30 | } 31 | 32 | vector> ans; 33 | 34 | while (!st.empty()) 35 | { 36 | ans.push_back(st.top()); 37 | st.pop(); 38 | } 39 | 40 | return ans; 41 | } 42 | 43 | int main() 44 | { 45 | } -------------------------------------------------------------------------------- /Stacks and Queues/minimu_sum_of_squares_of_char_counts_after_removing_k.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int minValue(string s, int k) 7 | { 8 | // code here 9 | int i, l; 10 | l = s.length(); 11 | if (k >= l) 12 | return 0; 13 | 14 | priority_queue qu; 15 | 16 | int hash[26] = {0}; 17 | 18 | for (i = 0; i < l; i++) 19 | hash[s[i] - 'a']++; 20 | 21 | for (i = 0; i < 26; i++) 22 | { 23 | if (hash[i] > 0) 24 | { 25 | qu.push(hash[i]); 26 | } 27 | } 28 | int temp; 29 | while (k--) 30 | { 31 | temp = qu.top(); 32 | qu.pop(); 33 | temp--; 34 | qu.push(temp); 35 | } 36 | int ans = 0; 37 | while (!qu.empty()) 38 | { 39 | temp = qu.top(); 40 | ans = ans + (temp * temp); 41 | qu.pop(); 42 | } 43 | 44 | return ans; 45 | } 46 | 47 | int main() 48 | { 49 | 50 | } -------------------------------------------------------------------------------- /Algorithms/Good_array_gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | Given an array nums of positive integers. Your task is to select some subset of nums, 4 | 5 | multiply each element by an integer and add all these numbers. 6 | The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand. 7 | 8 | Return True if the array is good otherwise return False 9 | */ 10 | 11 | int GCD(int a, int b) 12 | { 13 | 14 | if (b == 0) 15 | return a; 16 | 17 | if (b == 1) 18 | return 1; 19 | 20 | return GCD(b, a % b); 21 | } 22 | 23 | bool isGoodArray(vector&nums) 24 | { 25 | 26 | int i, prev; 27 | prev = nums[0]; 28 | 29 | for (i = 1; i < nums.size(); i++) 30 | { 31 | prev = GCD(prev, nums[i]); 32 | 33 | if (prev == 1) 34 | return true; 35 | } 36 | 37 | return prev == 1; 38 | } 39 | 40 | int main() 41 | { 42 | vectornums(10); 43 | 44 | isGoodArray(nums); 45 | } -------------------------------------------------------------------------------- /Algorithms/setprecision.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 5 | using namespace std; 6 | int main() 7 | { 8 | double pi = 3.14159, npi = -3.14159; 9 | cout< 2 | 3 | using namespace std; 4 | /* 5 | Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the 6 | elements in the subarray is strictly less than k. 7 | 8 | 9 | */ 10 | class Solution 11 | { 12 | public: 13 | int numSubarrayProductLessThanK(vector &nums, int k) 14 | { 15 | 16 | int n, start, end, prod = 1; 17 | n = nums.size(); 18 | 19 | start = 0; 20 | end = 0; 21 | int count = 0; 22 | while (end < n) 23 | { 24 | prod *= nums[end]; 25 | 26 | while (prod >= k && start <= end) 27 | { 28 | prod /= nums[start]; 29 | start++; 30 | } 31 | 32 | if (prod < k) 33 | count += (end - start) + 1; 34 | 35 | end++; 36 | } 37 | 38 | return count; 39 | } 40 | }; -------------------------------------------------------------------------------- /Arrays/pascal_triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | vector> generate(int numRows) 9 | { 10 | 11 | vector> ans; 12 | ans.push_back({1}); 13 | if (numRows == 1) 14 | return ans; 15 | 16 | int i = 2; 17 | 18 | while (i <= numRows) 19 | { 20 | if (i == 2) 21 | { 22 | ans.push_back({1, 1}); 23 | i++; 24 | continue; 25 | } 26 | 27 | vector v; 28 | v.push_back(1); 29 | 30 | vector prev = ans.back(); 31 | 32 | for (int j = 1; j < prev.size(); j++) 33 | { 34 | v.push_back(prev[j] + prev[j - 1]); 35 | } 36 | 37 | v.push_back(1); 38 | ans.push_back(v); 39 | i++; 40 | } 41 | 42 | return ans; 43 | } 44 | }; -------------------------------------------------------------------------------- /Graphs/Flood_fill_algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | void colour(int x, int y, int newColour, vector> &image, int src) 13 | { 14 | int n = image.size(); 15 | int m = image[0].size(); 16 | 17 | if (x < 0 || y < 0 || x >= n || y >= m || image[x][y] != src) 18 | return; 19 | 20 | image[x][y] = newColour; 21 | colour(x + 1, y, newColour, image, src); 22 | colour(x - 1, y, newColour, image, src); 23 | colour(x, y + 1, newColour, image, src); 24 | colour(x, y - 1, newColour, image, src); 25 | 26 | return; 27 | } 28 | 29 | vector> floodFill(vector> &image, int sr, int sc, int newColor) 30 | { 31 | // Code here 32 | 33 | colour(sr, sc, newColor, image, image[sr][sc]); 34 | 35 | return image; 36 | } 37 | 38 | int main() 39 | { 40 | 41 | } -------------------------------------------------------------------------------- /Stacks and Queues/Steps_to_make_arr_Non_decreasing_MonoTonciStack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | You are given a 0-indexed integer array nums. In one step, 7 | remove all elements nums[i] where nums[i - 1] > nums[i] for all 0 < i < nums.length. 8 | 9 | Return the number of steps performed until nums becomes a non-decreasing array. 10 | */ 11 | 12 | class Solution 13 | { 14 | public: 15 | int totalSteps(vector &nums) 16 | { 17 | int n = nums.size(), i, ans = 0, cnt, prev; 18 | stack> st; 19 | st.push({nums[n - 1], 0}); 20 | for (i = n - 2; i >= 0; i--) 21 | { 22 | cnt = 0; 23 | while (!st.empty() && nums[i] > st.top().first) 24 | { 25 | cnt = max(cnt + 1, st.top().second); 26 | st.pop(); 27 | } 28 | ans = max(ans, cnt); 29 | st.push({nums[i], cnt}); 30 | } 31 | return ans; 32 | } 33 | }; -------------------------------------------------------------------------------- /Dynamic-Programming/max_sum_partition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int solve(int ind, vector&v, int k, vector&Dp) 6 | { 7 | if(Dp[ind]!=-1) 8 | return Dp[ind]; 9 | 10 | 11 | int maxi = INT_MIN , maxAns = INT_MIN, sum; 12 | 13 | int len = 0; 14 | for(int i = ind ; i < min(ind+k , v.size()) ; i++) 15 | { 16 | len++; 17 | maxi = max(maxi , v[i]); 18 | sum = maxi*len + solve(ind+1,v,k,Dp); 19 | 20 | maxAns = max(maxAns,sum); 21 | 22 | } 23 | 24 | return Dp[ind] = maxAns; 25 | } 26 | int MaxPartitionSum(vector&V, int k) 27 | { 28 | int ind = 0; 29 | int n = V.size(); 30 | 31 | vectorDp(n,-1); 32 | return solve(ind,V,k,Dp); 33 | } 34 | int main() 35 | { 36 | int n; 37 | cin>>n; 38 | 39 | vectorV(n); 40 | 41 | for(int i = 0 ; i < n ; i++) 42 | cin>>V[i]; 43 | 44 | int k; 45 | cin>>k; 46 | 47 | cout<<"\n\n"< 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | 5 | using namespace std; 6 | 7 | int sum_subset(int a[],int n, int s) 8 | { 9 | int dp[n+1][s+1]; 10 | int i,j; 11 | for(i=0;i<=n;i++) 12 | { 13 | dp[i][0]=1; 14 | } 15 | 16 | for(i=1;i<=s;i++) 17 | { 18 | dp[0][i]=0; 19 | } 20 | 21 | for(i=1;i<=n;i++) 22 | { 23 | for(j=1;j<=s;j++) 24 | { 25 | if(j>n; 43 | cout<<"Enter the n elements\n"; 44 | int a[n]; 45 | for(i=0;i>a[i]; 48 | } 49 | cout<<"Enter the sum\n"; 50 | cin>>s; 51 | int ans=sum_subset(a,n,s); 52 | 53 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | void sorted_insert(stack&st, int ele) 7 | { 8 | if(st.empty() || ele>=st.top()) 9 | { 10 | st.push(ele); 11 | return; 12 | } 13 | 14 | int tp=st.top(); 15 | st.pop(); 16 | 17 | sorted_insert(st,ele); 18 | st.push(tp); 19 | 20 | } 21 | void Sort(stack&st) 22 | { 23 | if(st.size()==0) 24 | return; 25 | 26 | int ele=st.top(); 27 | st.pop(); 28 | Sort(st); 29 | sorted_insert(st,ele); 30 | } 31 | int main() 32 | { 33 | int n,i,x; 34 | cout<<"Enter the num og elements: "; 35 | cin>>n; 36 | 37 | cout<<"\nEnter the Elements : \n"; 38 | stackst; 39 | for(i=0;i>x; 42 | st.push(x); 43 | 44 | } 45 | Sort(st); 46 | cout<<"\n\n"; 47 | while(!st.empty()) 48 | { 49 | cout< 2 | 3 | using namespace std; 4 | 5 | int FindMedian(vector> &Mat) 6 | { 7 | int r, c; 8 | r = Mat.size(); 9 | c = Mat[0].size(); 10 | 11 | int desired = (r * c) / 2; 12 | 13 | int mini = INT_MAX, maxi = INT_MIN; 14 | 15 | for (int i = 0; i < r; i++) 16 | { 17 | if (mini > Mat[i][0]) 18 | mini = Mat[i][0]; 19 | 20 | if (maxi < Mat[i][c - 1]) 21 | maxi = Mat[i][c - 1]; 22 | } 23 | 24 | int mid, count; 25 | while (mini < maxi) 26 | { 27 | mid = (maxi + mini) / 2; 28 | 29 | count = 0; 30 | for (int i = 0; i < r; i++) 31 | { 32 | count += (upper_bound(Mat[i].begin(), Mat[i].end(), mid) - Mat[i].begin()); 33 | } 34 | 35 | if (count < desired) 36 | mini = mid + 1; 37 | else 38 | maxi = mid; 39 | } 40 | 41 | return mini; 42 | } 43 | 44 | int main() 45 | { 46 | } -------------------------------------------------------------------------------- /Arrays/next_permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | void nextPermutation(vector &nums) 9 | { 10 | 11 | int i, j; 12 | 13 | if (nums.size() == 1) 14 | return; 15 | 16 | for (i = nums.size() - 2; i >= 0; i--) 17 | { 18 | if (nums[i] < nums[i + 1]) 19 | { 20 | break; 21 | } 22 | } 23 | 24 | if (i < 0) 25 | { 26 | sort(nums.begin(), nums.end()); 27 | return; 28 | } 29 | else 30 | { 31 | j = nums.size() - 1; 32 | 33 | for (; j > i; j--) 34 | { 35 | if (nums[j] > nums[i]) 36 | break; 37 | } 38 | 39 | int t = nums[i]; 40 | nums[i] = nums[j]; 41 | nums[j] = t; 42 | 43 | reverse(nums.begin() + i + 1, nums.end()); 44 | } 45 | } 46 | }; -------------------------------------------------------------------------------- /Searching and sorting/Median_of_rowise_sorted_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int FindMedian(vector> &Mat) 6 | { 7 | int r, c; 8 | r = Mat.size(); 9 | c = Mat[0].size(); 10 | 11 | int desired = (r * c) / 2; 12 | 13 | int mini = INT_MAX, maxi = INT_MIN; 14 | 15 | for (int i = 0; i < r; i++) 16 | { 17 | if (mini > Mat[i][0]) 18 | mini = Mat[i][0]; 19 | 20 | if (maxi < Mat[i][c - 1]) 21 | maxi = Mat[i][c - 1]; 22 | } 23 | 24 | int mid, count; 25 | while (mini < maxi) 26 | { 27 | mid = (maxi + mini) / 2; 28 | 29 | count = 0; 30 | for (int i = 0; i < r; i++) 31 | { 32 | count += (upper_bound(Mat[i].begin(), Mat[i].end(), mid) - Mat[i].begin()); 33 | } 34 | 35 | if (count < desired) 36 | mini = mid + 1; 37 | else 38 | maxi = mid; 39 | } 40 | 41 | return mini; 42 | } 43 | 44 | int main() 45 | { 46 | } -------------------------------------------------------------------------------- /Algorithms/Extended_Euclid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | ax + by = gcd(a,b) 6 | gcd(a,b)=gcd(b,a%b) 7 | 8 | a%b= a - (a/b)*b 9 | 10 | bx1 + (a%b)y1 = ax + by 11 | 12 | bx1 + (a-(a/b)*b)y1= ax + by 13 | 14 | ax + by = ay1 + b(x1 - (a/b)*y1) 15 | 16 | hence, 17 | 18 | x=y1 19 | 20 | y= ( x1 - (a/b)*y1 ) 21 | 22 | 23 | */ 24 | struct triplet 25 | { 26 | int x; 27 | int y; 28 | int gcd; 29 | }; 30 | 31 | triplet Extended_Euclid(int a,int b) 32 | { 33 | triplet ans; 34 | 35 | if(b==0) 36 | { 37 | ans.x=1; 38 | ans.y=0; 39 | ans.gcd=a; 40 | return ans; 41 | } 42 | 43 | triplet smallAns=Extended_Euclid(b,a%b); 44 | 45 | ans.x=smallAns.y; 46 | ans.y=smallAns.x- (a/b)*smallAns.y; 47 | ans.gcd=smallAns.gcd; 48 | 49 | return ans; 50 | } 51 | 52 | int main() 53 | { 54 | int a,b; 55 | cin>>a>>b; 56 | 57 | triplet ans=Extended_Euclid(a,b); 58 | 59 | cout<<"x-> "< "< "< 2 | 3 | 4 | using namespace std; 5 | 6 | // This is an input class. Do not edit. 7 | class BST { 8 | public: 9 | int value; 10 | BST *left = nullptr; 11 | BST *right = nullptr; 12 | 13 | BST(int value) { this->value = value; } 14 | }; 15 | 16 | bool isAncestor(BST *src , BST *dst) 17 | { 18 | if(src==NULL) 19 | return false; 20 | 21 | while(src!=NULL) 22 | { 23 | if(src->value==dst->value) 24 | return true; 25 | 26 | if(src->value < dst->value) 27 | { 28 | src=src->right; 29 | } 30 | else 31 | { 32 | src=src->left; 33 | } 34 | } 35 | 36 | return false; 37 | } 38 | bool validateThreeNodes(BST *nodeOne, BST *nodeTwo, BST *nodeThree) { 39 | // Write your code here. 40 | 41 | if(isAncestor(nodeOne,nodeTwo) && isAncestor(nodeTwo,nodeThree)) 42 | return true; 43 | 44 | if(isAncestor(nodeThree,nodeTwo) && isAncestor(nodeTwo,nodeOne)) 45 | return true; 46 | 47 | 48 | return false; 49 | } 50 | 51 | int main() 52 | { 53 | 54 | } -------------------------------------------------------------------------------- /Greedy/activity_selection_greedy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | bool mycomp(vector&a, vector&b) 8 | { 9 | return a[1]>n; 17 | vector>V(n); 18 | cout<<"\nEnter start and end of n activites on separate lines : \n"; 19 | 20 | for(i=0;i>start; 23 | // cin>>end; 24 | // V.push_back({start,end}); 25 | // cin>>V[i][0]>>V[i][1]; 26 | vectora(2); 27 | cin>>a[0]; 28 | cin>>a[1]; 29 | V[i]=a; 30 | 31 | } 32 | 33 | sort(V.begin(),V.end(),mycomp); 34 | 35 | count=1; 36 | end=V[0][1]; 37 | 38 | for(i=1;i=end) 41 | { 42 | count++; 43 | end=V[i][1]; 44 | } 45 | } 46 | 47 | cout<<"\n"< 2 | #include 3 | 4 | using namespace std; 5 | 6 | // This is an input class. Do not edit. 7 | class BinaryTree { 8 | public: 9 | int value; 10 | BinaryTree *left = nullptr; 11 | BinaryTree *right = nullptr; 12 | 13 | BinaryTree(int value) { this->value = value; } 14 | }; 15 | 16 | 17 | void Traverse(BinaryTree *root, vector&v) 18 | { 19 | if(root==NULL) 20 | return; 21 | 22 | if(root->left==NULL && root->right==NULL) 23 | { 24 | v.push_back(root->value); 25 | return; 26 | } 27 | 28 | Traverse(root->left,v); 29 | Traverse(root->right,v); 30 | } 31 | 32 | bool compareLeafTraversal(BinaryTree *tree1, BinaryTree *tree2) { 33 | // Write your code here. 34 | if(tree1==NULL && tree2==NULL) 35 | return true; 36 | 37 | if(tree1==NULL || tree2==NULL) 38 | return false; 39 | vectorv1; 40 | vectorv2; 41 | 42 | Traverse(tree1,v1); 43 | Traverse(tree2,v2); 44 | 45 | if(v1==v2) 46 | return true; 47 | else 48 | return false; 49 | } 50 | -------------------------------------------------------------------------------- /Link-Lists/sort_LL_of_0s_1s_2s.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | 11 | Node(int x = 0) 12 | { 13 | data = x; 14 | next = NULL; 15 | } 16 | }; 17 | 18 | Node *segregate(Node *head) 19 | { 20 | 21 | // Add code here 22 | int count[3] = {0}; 23 | 24 | Node *t = head; 25 | 26 | while (t) 27 | { 28 | count[t->data]++; 29 | 30 | t = t->next; 31 | } 32 | 33 | t = head; 34 | 35 | while (t) 36 | { 37 | if (count[0] > 0) 38 | { 39 | t->data = 0; 40 | count[0]--; 41 | } 42 | else if (count[1] > 0) 43 | { 44 | t->data = 1; 45 | count[1]--; 46 | } 47 | else if (count[2] > 0) 48 | { 49 | t->data = 2; 50 | count[2]--; 51 | } 52 | 53 | t = t->next; 54 | } 55 | 56 | return head; 57 | } 58 | 59 | int main() 60 | { 61 | } 62 | -------------------------------------------------------------------------------- /Algorithms/sum_divisible_by.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | You are given a list of songs where the ith song has a duration of time[i] seconds. 6 | 7 | Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, 8 | we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0. 9 | */ 10 | class Solution 11 | { 12 | public: 13 | int numPairsDivisibleBy60(vector &time) 14 | { 15 | 16 | for (int i = 0; i < time.size(); i++) 17 | time[i] %= 60; 18 | 19 | vector hash(60, 0); 20 | 21 | for (int i = 0; i < time.size(); i++) 22 | hash[time[i]]++; 23 | 24 | int count = 0; 25 | 26 | int i = 1; 27 | 28 | while (i < 30) 29 | { 30 | count += (hash[i] * hash[60 - i]); 31 | i++; 32 | } 33 | 34 | count += (hash[0] * (hash[0] - 1)) / 2; 35 | 36 | count += (hash[30] * (hash[30] - 1)) / 2; 37 | 38 | return count; 39 | } 40 | }; -------------------------------------------------------------------------------- /Arrays/moores_voting_algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findmajority(int a[],int n) 5 | { 6 | int count=1; 7 | int maj_index=0; 8 | int i; 9 | for(i=1;in/2) 38 | return 1; 39 | else 40 | return 0; 41 | } 42 | 43 | int main() { 44 | int n,i; 45 | cin>>n; 46 | int a[n]; 47 | for(i=0;i>a[i]; 50 | } 51 | int x=findmajority(a,n); 52 | 53 | 54 | if(ismaj(a,n,x)) 55 | cout< 2 | #include 3 | 4 | using namespace std; 5 | 6 | void insertAtBottom(stack&st, int ele) 7 | { 8 | if(st.empty()) 9 | { 10 | st.push(ele); 11 | return; 12 | } 13 | 14 | int tp=st.top(); 15 | st.pop(); 16 | insertAtBottom(st,ele); 17 | st.push(tp); 18 | } 19 | 20 | void reverse_stack(stack&st) 21 | { 22 | if(st.empty()) 23 | return; 24 | 25 | int ele=st.top(); 26 | st.pop(); 27 | 28 | reverse_stack(st); 29 | insertAtBottom(st, ele); 30 | } 31 | 32 | int main() 33 | { 34 | stackst; 35 | 36 | st.push(1); 37 | st.push(2); 38 | st.push(3); 39 | st.push(4); 40 | st.push(5); 41 | 42 | reverse_stack(st); 43 | 44 | cout< 2 | using namespace std; 3 | 4 | int findmajority(int a[],int n) 5 | { 6 | int count=1; 7 | int maj_index=0; 8 | int i; 9 | for(i=1;in/2) 38 | return 1; 39 | else 40 | return 0; 41 | } 42 | 43 | int main() { 44 | int n,i; 45 | cin>>n; 46 | int a[n]; 47 | for(i=0;i>a[i]; 50 | } 51 | int x=findmajority(a,n); 52 | 53 | 54 | if(ismaj(a,n,x)) 55 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | cout << "Enter the num of Elements : "; 8 | int i, j, k, n; 9 | cin >> n; 10 | int a[n]; 11 | 12 | cout << "Enter the n Elements : "; 13 | 14 | for (i = 0; i < n; i++) 15 | { 16 | cin >> a[i]; 17 | } 18 | 19 | int LIS[n] = {1}, LDS[n] = {1}; 20 | 21 | for (i = 1; i < n; i++) 22 | { 23 | for (j = 0; j < i; j++) 24 | { 25 | if (a[j] < a[i]) 26 | { 27 | LIS[i] = max(LIS[i], 1 + LIS[j]); 28 | } 29 | } 30 | } 31 | 32 | for (i = n - 2; i >= 0; i--) 33 | { 34 | for (j = n - 1; j > i; j--) 35 | { 36 | if (a[i] > a[j]) 37 | { 38 | LDS[i] = max(LDS[i], 1 + LDS[j]); 39 | } 40 | } 41 | } 42 | int ans = 1; 43 | for (i = 0; i < n; i++) 44 | { 45 | ans = max(ans, LIS[i] + LDS[i] - 1); 46 | } 47 | 48 | cout << ans; 49 | } -------------------------------------------------------------------------------- /Graphs/bfs_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | 9 | cin>>n; 10 | 11 | vectora(n); 12 | for(int i = 0 ; i < n ; i++) 13 | { 14 | cin>>a[i]; 15 | } 16 | 17 | vectorjumps(n,INT_MAX); 18 | 19 | vectorpos(n,-1); 20 | 21 | jumps[0] = 0; 22 | 23 | for(int i = 1 ; i < n ; i++) 24 | { 25 | jumps[i] = INT_MAX; 26 | for(int j = 0 ; j < i ; j++) 27 | { 28 | if(j+a[j] >= i && jumps[j]!=INT_MAX) 29 | { 30 | if(jumps[j]+1 < jumps[i]) 31 | { 32 | jumps[i] = jumps[j]+1; 33 | pos[i] = j; 34 | } 35 | } 36 | } 37 | } 38 | 39 | 40 | int sum = a[n-1]; 41 | 42 | int k = pos[n-1]; 43 | 44 | while(k>=0) 45 | { 46 | sum += a[k]; 47 | k = pos[k]; 48 | } 49 | 50 | if(jumps[n-1] == INT_MAX) 51 | cout<<"-1\n"; 52 | else 53 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int get_count(Node *h) 7 | { 8 | if(h==NULL) 9 | return 0; 10 | 11 | return 1+get_count(h->next); 12 | } 13 | 14 | 15 | int intersectPoint(Node* head1, Node* head2) 16 | { 17 | // Your Code Here 18 | int c1,c2; 19 | 20 | c1=get_count(head1); 21 | c2=get_count(head2); 22 | 23 | Node *h1,*h2; 24 | 25 | if(c1>c2) 26 | { 27 | h1=head1; 28 | h2=head2; 29 | } 30 | else 31 | { 32 | h1=head2; 33 | h2=head1; 34 | } 35 | 36 | int d=abs(c1-c2); 37 | 38 | int i=0; 39 | while(inext; 42 | i++; 43 | } 44 | 45 | while(h1!=NULL && h2!=NULL) 46 | { 47 | if(h1==h2) 48 | { 49 | return h1->data; 50 | } 51 | else 52 | { 53 | h1=h1->next; 54 | h2=h2->next; 55 | } 56 | } 57 | } 58 | 59 | int main() 60 | { 61 | 62 | } -------------------------------------------------------------------------------- /Stacks and Queues/Largest_area_in_histogram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int Largest_Rect(vector&Hist, int n) 8 | { 9 | stackst; 10 | int i,j; 11 | Hist.push_back(0); 12 | n=Hist.size(); 13 | int area=0; 14 | while(i>n; 38 | vectorHist(n); 39 | cout<<"\nEnter the Elements of Histogram : \n"; 40 | for(i=0;i>Hist[i]; 43 | } 44 | 45 | cout<<"Area of largest is : "< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Node 9 | { 10 | public: 11 | int data; 12 | Node *left; 13 | Node *right; 14 | 15 | Node(int d=0) 16 | { 17 | data=d; 18 | left=NULL; 19 | right=NULL; 20 | } 21 | }; 22 | 23 | vector reverseLevelOrder(Node *root) 24 | { 25 | // code here 26 | vectorans; 27 | 28 | queuequ; 29 | stackst; 30 | 31 | qu.push(root); 32 | 33 | while(!qu.empty()) 34 | { 35 | Node *t=qu.front(); 36 | qu.pop(); 37 | 38 | st.push(t->data); 39 | 40 | if(t->right) 41 | qu.push(t->right); 42 | 43 | if(t->left) 44 | qu.push(t->left); 45 | } 46 | 47 | while(!st.empty()) 48 | { 49 | ans.push_back(st.top()); 50 | st.pop(); 51 | } 52 | 53 | 54 | return ans; 55 | } 56 | 57 | int main() 58 | { 59 | 60 | } -------------------------------------------------------------------------------- /Graphs/BFS_graph_travrse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 100 4 | using namespace std; 5 | 6 | void BFS(int G[][SIZE], int start ,int n) 7 | { 8 | queuequ; 9 | int u,v; 10 | int visited[n]={0}; 11 | cout<>n; 37 | 38 | int G[SIZE][SIZE]; 39 | cout<<"Enter the Graph in form of adjency matrix (vertives start from 0) : \n"; 40 | for(i=0;i>G[i][j]; 45 | } 46 | } 47 | 48 | BFS(G,0,n); 49 | } -------------------------------------------------------------------------------- /Searching and sorting/sort_array_according_to_num_of_set_bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int count_bits(int n) 8 | { 9 | int count=0; 10 | 11 | while(n>0) 12 | { 13 | if(n&1) 14 | count++; 15 | 16 | n=n>>1; 17 | } 18 | 19 | return count; 20 | } 21 | 22 | 23 | 24 | void SortBy_setBits(int a[], int n) 25 | { 26 | vector>V(n,vector(2,0)); 27 | 28 | int i,j; 29 | for(i=0;i>n; 50 | int a[n]; 51 | cout<<"\nEnter the Elements : \n"; 52 | 53 | for(i=0;i>a[i]; 55 | 56 | 57 | SortBy_setBits(a,n); 58 | } -------------------------------------------------------------------------------- /Arrays/Get_a_sum_in_four_arrays_IMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | /* 7 | Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 8 | 9 | 0 <= i, j, k, l < n 10 | nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 11 | 12 | 13 | 14 | */ 15 | class Solution 16 | { 17 | public: 18 | int fourSumCount(vector &nums1, vector &nums2, vector &nums3, vector &nums4) 19 | { 20 | 21 | unordered_map abSum; 22 | 23 | for (auto a : nums1) 24 | { 25 | for (auto b : nums2) 26 | { 27 | abSum[a + b]++; 28 | } 29 | } 30 | 31 | int count = 0; 32 | 33 | for (auto c : nums3) 34 | { 35 | for (auto d : nums4) 36 | { 37 | auto it = abSum.find(0 - c - d); 38 | 39 | if (it != abSum.end()) 40 | count += (it->second); 41 | } 42 | } 43 | 44 | return count; 45 | } 46 | }; -------------------------------------------------------------------------------- /Searching and sorting/Find_majority_ele_moor_voting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findmajority(int a[],int n) 5 | { 6 | int count=1; 7 | int maj_index=0; 8 | int i; 9 | for(i=1;in/2) 38 | return 1; 39 | else 40 | return 0; 41 | } 42 | 43 | int main() { 44 | int n,i; 45 | cin>>n; 46 | int a[n]; 47 | for(i=0;i>a[i]; 50 | } 51 | int x=findmajority(a,n); 52 | 53 | 54 | if(ismaj(a,n,x)) 55 | cout< 2 | 3 | using namespace std; 4 | /* 5 | For two strings s and t, we say "t divides s" if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times). 6 | 7 | Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2. 8 | */ 9 | class Solution 10 | { 11 | public: 12 | string gcdOfStrings(string str1, string str2) 13 | { 14 | 15 | if (str1 + str2 != str2 + str1) 16 | return ""; 17 | 18 | int l1 = str1.length(); 19 | int l2 = str2.length(); 20 | 21 | int g = __gcd(l1, l2); 22 | 23 | return str1.substr(0, g); 24 | } 25 | 26 | //Approach 2 27 | string gcdOfStrings1(string str1, string str2) 28 | { 29 | 30 | if (str1.size() < str2.size()) 31 | return gcdOfStrings(str2, str1); 32 | if (str2.empty()) 33 | return str1; 34 | if (str1.substr(0, str2.size()) != str2) 35 | return ""; 36 | return gcdOfStrings(str1.substr(str2.size()), str2); 37 | } 38 | }; -------------------------------------------------------------------------------- /Strings/min_chars_to_insert_at_begin_to_make_it_palindrome_IMP_LPS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | Given an string A. The only operation allowed is to insert characters in the beginning of the string. 7 | 8 | Find how many minimum characters are needed to be inserted to make the string a palindrome string. 9 | 10 | */ 11 | 12 | int solve(string A) { 13 | 14 | string B = A; 15 | reverse(B.begin(),B.end()); 16 | 17 | string pal = A+"#"+B; 18 | 19 | int n,i,j,m; 20 | 21 | m = pal.length(); 22 | n = A.length(); 23 | 24 | vectorlps(m,0); 25 | 26 | for(i=1;i0 && pal[i]!=pal[j]) 32 | j = lps[j-1]; 33 | 34 | 35 | if(pal[i] == pal[j]) 36 | j++; 37 | 38 | lps[i] = j; 39 | } 40 | 41 | cout<>A; 52 | 53 | cout<<"\n"< 2 | 3 | using namespace std; 4 | /* 5 | Given an integer array nums and an integer k, return true if nums has a continuous subarray of 6 | size at least two whose elements sum up to a multiple of k, or false otherwise. 7 | 8 | An integer x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k. 9 | */ 10 | class Solution 11 | { 12 | public: 13 | bool checkSubarraySum(vector &nums, int k) 14 | { 15 | int runningSum = 0; 16 | 17 | unordered_map mp; 18 | 19 | mp[0] = -1; 20 | 21 | for (int i = 0; i < nums.size(); i++) 22 | { 23 | runningSum += nums[i]; 24 | 25 | if (k != 0) 26 | runningSum %= k; 27 | 28 | if (mp.find(runningSum) != mp.end()) 29 | { 30 | if (i - mp[runningSum] > 1) 31 | return true; 32 | } 33 | else 34 | mp[runningSum] = i; 35 | } 36 | 37 | return false; 38 | } 39 | }; -------------------------------------------------------------------------------- /Greedy/Tandem_speed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int tandemBicycle(vector red, vector blue, 8 | bool fastest) { 9 | // Write your code here. 10 | sort(red.begin(),red.end()); 11 | sort(blue.begin(),blue.end()); 12 | int totalspeed; 13 | int r=red.size(); 14 | int b=blue.size(); 15 | if(fastest) 16 | { 17 | int i,j,m,n; 18 | int speed=0; 19 | i=r-1; 20 | j=b-1; 21 | 22 | n=0; //red 23 | m=0; //blue 24 | 25 | while(i>=n && j>=m) 26 | { 27 | if(red[i] > blue[j]) 28 | { 29 | speed+=red[i]; 30 | i--; 31 | m++; 32 | } 33 | else 34 | { 35 | speed+=blue[j]; 36 | j--; 37 | n++; 38 | } 39 | } 40 | 41 | return speed; 42 | } 43 | else 44 | { 45 | int i,j,m,n; 46 | int speed=0; 47 | i=r-1; 48 | j=b-1; 49 | 50 | n=0; //red 51 | m=0; //blue 52 | 53 | while(i>=0) 54 | { 55 | speed+=(max(red[i],blue[i])); 56 | i--; 57 | } 58 | 59 | return speed; 60 | } 61 | return -1; 62 | } 63 | -------------------------------------------------------------------------------- /Algorithms/Multiplicative_modulo_inverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | (A * B) % m =1 7 | 8 | A and m are given find B 9 | 10 | for multipicative modulo inverse to exist gcd(A,m)=1 11 | 12 | (A*B)+ qm = 1 13 | same to exteded Euclid 14 | where x=B 15 | y=q 16 | GCD=1 17 | 18 | */ 19 | 20 | struct triplet 21 | { 22 | int x; 23 | int y; 24 | int gcd; 25 | }; 26 | 27 | triplet Extended_Euclid(int a,int b) 28 | { 29 | triplet ans; 30 | 31 | if(b==0) 32 | { 33 | ans.x=1; 34 | ans.y=0; 35 | ans.gcd=a; 36 | return ans; 37 | } 38 | 39 | triplet smallAns=Extended_Euclid(b,a%b); 40 | 41 | ans.x=smallAns.y; 42 | ans.y=smallAns.x- (a/b)*smallAns.y; 43 | ans.gcd=smallAns.gcd; 44 | 45 | return ans; 46 | } 47 | 48 | int Multiplicative_modulo_inverse(int A, int m) 49 | { 50 | triplet ans=Extended_Euclid(A,m); 51 | return ans.x; 52 | } 53 | 54 | int main() 55 | { 56 | int A,m; 57 | int B=Multiplicative_modulo_inverse(A,m); 58 | 59 | cout<<"\nB -> "< 2 | 3 | using namespace std; 4 | /* 5 | Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) such that: 6 | 7 | 0 <= i < j <= n - 1 and 8 | nums[i] * nums[j] is divisible by k. 9 | */ 10 | class Solution 11 | { 12 | public: 13 | long long countPairs(vector &nums, int k) 14 | { 15 | 16 | unordered_map mp; 17 | 18 | long long res = 0; 19 | 20 | int n = nums.size(); 21 | for (int i = 0; i < n; i++) 22 | { 23 | int gcd1 = __gcd(nums[i], k); 24 | int gcd2 = (k / gcd1); 25 | 26 | if (gcd2 == 1) 27 | { 28 | res += i; 29 | } 30 | else 31 | { 32 | for (auto it : mp) 33 | { 34 | if (it.first % gcd2 == 0) 35 | res += it.second; 36 | } 37 | } 38 | 39 | mp[gcd1]++; 40 | } 41 | 42 | return res; 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /Stacks and Queues/postfix_Evaluation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int postfixVal(string s) 8 | { 9 | stackst; 10 | 11 | for(int i=0;i='0' && s[i]<='9') 14 | { 15 | st.push(s[i]-'0'); 16 | } 17 | else 18 | { 19 | int op1, op2; 20 | op2=st.top(); 21 | st.pop(); 22 | op1=st.top(); 23 | st.pop(); 24 | 25 | int ans; 26 | 27 | if(s[i]=='+') 28 | ans=op1+op2; 29 | else if(s[i]=='-') 30 | ans=op1-op2; 31 | else if(s[i]=='*') 32 | ans=op1*op2; 33 | else if(s[i]=='/') 34 | ans=op1/op2; 35 | 36 | st.push(ans); 37 | } 38 | } 39 | 40 | int a=st.top(); 41 | return a; 42 | } 43 | int main() 44 | { 45 | string s; 46 | cout<<"Enter the postfix Expression : "; 47 | cin>>s; 48 | cout<<"\nExpression value is : "< 2 | #define mod 1000000007 3 | using namespace std; 4 | 5 | vectordp(10002,-1); 6 | 7 | 8 | long long FormPartitions(string s, int idx) 9 | { 10 | if(idx >= s.length()) 11 | return 0; 12 | 13 | if(dp[idx]!=-1) 14 | return dp[idx]; 15 | 16 | 17 | int zeros = 0; 18 | long long ops = 0, operations = INT_MAX; 19 | for(int i = idx ; i < s.length() ; i = i + 2) 20 | { 21 | if(s[i] == '0') 22 | zeros++; 23 | 24 | if(s[i+1] == '0') 25 | zeros++; 26 | 27 | ops = min(zeros , (i+1-idx+1 - zeros)); 28 | 29 | ops = (ops%mod) + FormPartitions(s,i+2) % mod; 30 | 31 | operations = min(ops,operations); 32 | } 33 | 34 | return dp[idx] = operations % mod; 35 | } 36 | 37 | 38 | vector vikingSubstring(string s) 39 | { 40 | vectorans(2); 41 | 42 | int n = s.length(); 43 | 44 | long B,K; 45 | 46 | long long B = FormPartitions(s,0); 47 | 48 | cout< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int wiggleMaxLength(vector &nums) 9 | { 10 | 11 | int n = nums.size(); 12 | 13 | vector up(n, 0); 14 | vector down(n, 0); 15 | 16 | up[0] = 1; 17 | down[0] = 1; 18 | 19 | for (int i = 1; i < n; i++) 20 | { 21 | if (nums[i] > nums[i - 1]) 22 | { 23 | up[i] = down[i - 1] + 1; 24 | down[i] = down[i - 1]; 25 | } 26 | else if (nums[i] < nums[i - 1]) 27 | { 28 | down[i] = up[i - 1] + 1; 29 | up[i] = up[i - 1]; 30 | } 31 | else 32 | { 33 | up[i] = up[i - 1]; 34 | down[i] = down[i - 1]; 35 | } 36 | } 37 | 38 | return max(down[n - 1], up[n - 1]); 39 | } 40 | }; 41 | 42 | //https://leetcode.com/problems/wiggle-subsequence/discuss/84843/Easy-understanding-DP-solution-with-O(n)-Java-version -------------------------------------------------------------------------------- /Link-Lists/Check_if_list_is_palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Definition for singly-linked list. 6 | struct ListNode 7 | { 8 | int val; 9 | ListNode *next; 10 | ListNode() : val(0), next(nullptr) {} 11 | ListNode(int x) : val(x), next(nullptr) {} 12 | ListNode(int x, ListNode *next) : val(x), next(next) {} 13 | }; 14 | 15 | class Solution 16 | { 17 | public: 18 | ListNode *temp; 19 | 20 | bool check(ListNode *p) 21 | { 22 | if (p == NULL) 23 | return true; 24 | 25 | bool isPal = check(p->next) & (p->val == temp->val); 26 | temp = temp->next; 27 | 28 | return isPal; 29 | } 30 | bool isPalindrome(ListNode *head) 31 | { 32 | 33 | if (!head || !head->next) 34 | return true; 35 | 36 | temp = head; 37 | 38 | return check(head); 39 | } 40 | }; 41 | 42 | 43 | // Another aproach 44 | 45 | /* 46 | Traverse to the mid of the link list using fast and slow ptr technique and then reverse the link list from the mid. 47 | and then compare the 2 halves. 48 | */ -------------------------------------------------------------------------------- /Algorithms/reduce_to_zero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define inf 10000 3 | using namespace std; 4 | 5 | 6 | int count(int a[], int l,int h, int x) 7 | { 8 | if(x==0) 9 | return 0; 10 | 11 | if(l==h && x==a[l]) 12 | { 13 | return 1; 14 | } 15 | else if(l==h && x>a[l]) 16 | { 17 | return inf; 18 | } 19 | 20 | if(lx && a[h]<=x) 23 | { 24 | return 1+count(a,l,h-1,x-a[h]); 25 | } 26 | else if(a[l]<=h && a[h]>x) 27 | { 28 | return 1+count(a,l+1,h,x-a[l]); 29 | } 30 | else if(a[l]>x && a[h]>x) 31 | { 32 | return inf; 33 | } 34 | else if(a[l]<=h && a[h]<=x) 35 | { 36 | return 1+min(count(a,l+1,h,x-a[l]),count(a,l,h-1,x-a[h])); 37 | } 38 | } 39 | } 40 | 41 | int main() { 42 | int n,x,i; 43 | cin>>n; 44 | int a[n]; 45 | for(i=0;i>a[i]; 48 | } 49 | cin>>x; 50 | int s=count(a,0,n-1,x); 51 | if(s>=inf) 52 | cout<<-1; 53 | else 54 | cout< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | vector targetIndices(vector &nums, int target) 9 | { 10 | 11 | // sort(nums.begin(),nums.end()); 12 | 13 | // vectorans; 14 | 15 | // for(int i=0;i ans; 24 | 25 | int less_than = 0; 26 | int tgt_cnt = 0; 27 | for (int i = 0; i < nums.size(); i++) 28 | { 29 | if (nums[i] < target) 30 | less_than++; 31 | else if (nums[i] == target) 32 | tgt_cnt++; 33 | } 34 | 35 | int i = less_than; 36 | while (tgt_cnt) 37 | { 38 | ans.push_back(i); 39 | i++; 40 | tgt_cnt--; 41 | } 42 | 43 | return ans; 44 | } 45 | }; -------------------------------------------------------------------------------- /Strings/Remove_duplicates_from_strings_IMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | Given a string s, remove duplicate letters so that every letter appears once and only once. 6 | You must make sure your result is the smallest in lexicographical order among all possible results. 7 | 8 | 9 | */ 10 | class Solution 11 | { 12 | public: 13 | string removeDuplicateLetters(string s) 14 | { 15 | 16 | string ans = "0"; 17 | 18 | vector cnt(256, 0); 19 | 20 | for (auto c : s) 21 | cnt[c]++; 22 | 23 | vector visited(256, false); 24 | for (auto c : s) 25 | { 26 | cnt[c]--; 27 | 28 | if (visited[c]) 29 | continue; 30 | 31 | while (c < ans.back() && cnt[ans.back()] > 0) 32 | { 33 | visited[ans.back()] = false; 34 | // cnt[ans.back()]++; 35 | 36 | ans.pop_back(); 37 | } 38 | 39 | ans += c; 40 | visited[c] = true; 41 | } 42 | 43 | return ans.substr(1); 44 | } 45 | }; -------------------------------------------------------------------------------- /solving/swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,m,i,j; 8 | 9 | cin>>n; 10 | vectora(n); 11 | int sa=0,sb=0; 12 | int ea=0,eb=0,oa=0,ob=0; 13 | for(i=0;i>a[i]; 16 | 17 | sa+=a[i]; 18 | 19 | if(a[i]&1) 20 | oa++; 21 | else 22 | ea++; 23 | } 24 | 25 | cin>>m; 26 | 27 | vectorb(m); 28 | 29 | for(i=0;i>b[i]; 32 | 33 | sb+=b[i]; 34 | 35 | if(b[i]&1) 36 | ob++; 37 | else 38 | eb++; 39 | } 40 | 41 | if(sb%2==0 && sa%2==0) 42 | cout<<"0\n"; 43 | else if(sb%2==1 && sa%2==1) 44 | { 45 | if(oa>0 && eb>0) 46 | cout<<"1\n"; 47 | else if(ob>0 && ea>0) 48 | cout<<"1\n"; 49 | else 50 | cout<<"-1\n"; 51 | } 52 | else if(sa%2==0 && sb%2==1) 53 | { 54 | cout<<"-1\n"; 55 | } 56 | else if(sa%2==1 && sb%2==0) 57 | { 58 | cout<<"-1\n"; 59 | } 60 | 61 | 62 | } -------------------------------------------------------------------------------- /Stacks and Queues/get_min_stack_in_space_efficient.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | class Stack 10 | { 11 | public: 12 | int minEle; 13 | stackst; 14 | 15 | Stack() 16 | { 17 | minEle=INT_MAX; 18 | } 19 | 20 | void Push(int x) 21 | { 22 | if(x>=minEle) 23 | { 24 | st.push(x); 25 | return; 26 | } 27 | 28 | if(x=minEle) 45 | return y; 46 | 47 | if(y 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Node 9 | { 10 | public: 11 | int data; 12 | Node *left; 13 | Node *right; 14 | 15 | Node(int d = 0) 16 | { 17 | data = d; 18 | left = NULL; 19 | right = NULL; 20 | } 21 | }; 22 | 23 | vector bottomView(Node *root) 24 | { 25 | // Your Code Here 26 | mapmp; 27 | queue>qu; 28 | 29 | int hd; 30 | Node *p; 31 | qu.push({root,0}); 32 | 33 | while(!qu.empty()) 34 | { 35 | p=qu.front().first; 36 | hd=qu.front().second; 37 | 38 | mp[hd]=p->data; 39 | 40 | if(p->left) 41 | qu.push({p->left,hd-1}); 42 | 43 | if(p->right) 44 | qu.push({p->right,hd+1}); 45 | 46 | 47 | qu.pop(); 48 | } 49 | 50 | vectorans; 51 | 52 | for(auto it : mp) 53 | ans.push_back(it.second); 54 | 55 | return ans; 56 | } 57 | 58 | int main() 59 | { 60 | 61 | } -------------------------------------------------------------------------------- /Arrays/min_homecoing_cost_grid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int minCost(vector &startPos, vector &homePos, vector &rowCosts, vector &colCosts) 9 | { 10 | 11 | int r1 = min(startPos[0], homePos[0]); 12 | int r2 = max(startPos[0], homePos[0]); 13 | 14 | int c1 = min(startPos[1], homePos[1]); 15 | int c2 = max(startPos[1], homePos[1]); 16 | 17 | int cost = 0; 18 | if (r1 == startPos[0]) 19 | { 20 | for (int i = r1 + 1; i <= r2; i++) 21 | cost += rowCosts[i]; 22 | } 23 | else 24 | { 25 | for (int i = r2 - 1; i >= r1; i--) 26 | cost += rowCosts[i]; 27 | } 28 | 29 | if (c1 == startPos[1]) 30 | { 31 | for (int i = c1 + 1; i <= c2; i++) 32 | cost += colCosts[i]; 33 | } 34 | else 35 | { 36 | for (int i = c2 - 1; i >= c1; i--) 37 | cost += colCosts[i]; 38 | } 39 | 40 | return cost; 41 | } 42 | }; -------------------------------------------------------------------------------- /Dynamic-Programming/longest_common_subsequence(DYNAMIC).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int lcs(int x, int y, string s1, string s2) 7 | { 8 | 9 | // your code here 10 | int **dp; 11 | int i,j; 12 | dp=new int*[x+1]; 13 | 14 | for(i=0;i>x>>y; 44 | cout<<"Enter the 1st string\n"; 45 | cin>>s1; 46 | cout<<"Enter the 2nd string\n"; 47 | cin>>s2; 48 | x=s1.length(); 49 | y=s2.length(); 50 | cout< 2 | 3 | using namespace std; 4 | // BT19CSE012 Onkar Bartakke 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *next; 10 | Node *bottom; 11 | 12 | Node(int x = 0) 13 | { 14 | data = x; 15 | next = NULL; 16 | } 17 | }; 18 | 19 | 20 | Node* merge(Node *h1, Node *h2) 21 | { 22 | if(h1==NULL) 23 | return h2; 24 | if(h2==NULL) 25 | return h1; 26 | 27 | Node *result; 28 | 29 | if(h1->data < h2->data) 30 | { 31 | result=h1; 32 | result->bottom=merge(h1->bottom, h2); 33 | } 34 | else 35 | { 36 | result=h2; 37 | result->bottom=merge(h1,h2->bottom); 38 | } 39 | 40 | return result; 41 | } 42 | 43 | 44 | Node* flatten(Node *root) 45 | { 46 | // Your code here 47 | if(root==NULL || root->next==NULL) 48 | return root; 49 | 50 | Node *h1,*h2; 51 | h1=root; 52 | h2=flatten(root->next); 53 | 54 | h1->next=NULL; 55 | 56 | return merge(h1,h2); 57 | 58 | } 59 | 60 | int main() 61 | { 62 | 63 | } -------------------------------------------------------------------------------- /Stacks and Queues/Trapping_Rainwater.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int Trapped_RainWater(vector&a, int n) 8 | { 9 | stackst; 10 | int i,j; 11 | int ans=0; 12 | for(i=0;i>n; 42 | vectorheight(n); 43 | cout<<"\nEnter the Heights of Boxes : \n"; 44 | for(i=0;i>height[i]; 47 | } 48 | 49 | cout<<"\nTrapped Rainwater is : "< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // This is the class of the input root. Do not edit it. 8 | class BinaryTree 9 | { 10 | public: 11 | int value; 12 | BinaryTree *left; 13 | BinaryTree *right; 14 | 15 | BinaryTree(int value) 16 | { 17 | this->value = value; 18 | left = nullptr; 19 | right = nullptr; 20 | } 21 | }; 22 | 23 | vector ans; 24 | 25 | void branchsumsUntil(BinaryTree *root, int sum) 26 | { 27 | if (root == NULL) 28 | return; 29 | 30 | if (root->left == NULL && root->right == NULL) 31 | { 32 | ans.push_back(sum); 33 | return; 34 | } 35 | 36 | if (root->left) 37 | branchsumsUntil(root->left, sum + root->left->value); 38 | 39 | if (root->right) 40 | branchsumsUntil(root->right, sum + root->right->value); 41 | } 42 | vector branchSums(BinaryTree *root) 43 | { 44 | // Write your code here. 45 | ans.clear(); 46 | branchsumsUntil(root, root->value); 47 | return ans; 48 | } 49 | 50 | int main() 51 | { 52 | } 53 | -------------------------------------------------------------------------------- /Searching and sorting/book_allocation(minimize_max_pages).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool max_page(int mid, int m, int n,int p[]) 5 | { 6 | int i,student,book=0; 7 | student=1; 8 | for(i=0;imid) 11 | { 12 | student++; 13 | book=p[i]; 14 | if(student>m) 15 | return false; 16 | } 17 | else 18 | { 19 | book+=p[i]; 20 | } 21 | } 22 | 23 | 24 | return true; 25 | } 26 | 27 | int main() { 28 | int n,m,i; 29 | cin>>n; 30 | int p[n]; 31 | int high,low,mid; 32 | high=0; 33 | low=p[0]; 34 | for(i=0;i>p[i]; 37 | high+=p[i]; 38 | } 39 | int ans=low; 40 | while(low 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class Node 11 | { 12 | public: 13 | int data; 14 | Node *left; 15 | Node *right; 16 | 17 | Node(int d = 0) 18 | { 19 | data = d; 20 | left = NULL; 21 | right = NULL; 22 | } 23 | }; 24 | 25 | int sumTree(Node *root) 26 | { 27 | if (root == NULL) 28 | return 0; 29 | 30 | if (root->left == NULL && root->right == NULL) 31 | { 32 | int s = root->data; 33 | root->data = 0; 34 | 35 | return s; 36 | } 37 | 38 | int s = root->data; 39 | int left = sumTree(root->left); 40 | int right = sumTree(root->right); 41 | 42 | root->data = (left + right); 43 | 44 | return (s + root->data); 45 | } 46 | void toSumTree(Node *node) 47 | { 48 | // Your code here 49 | int left = sumTree(node->left); 50 | 51 | int right = sumTree(node->right); 52 | 53 | node->data = (left + right); 54 | } 55 | 56 | int main() 57 | { 58 | 59 | } -------------------------------------------------------------------------------- /Trees/Diagonal_Traversal_of_a_BT.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Node 9 | { 10 | public: 11 | int data; 12 | Node *left; 13 | Node *right; 14 | 15 | Node(int d = 0) 16 | { 17 | data = d; 18 | left = NULL; 19 | right = NULL; 20 | } 21 | }; 22 | void Traverse(Node* root, int d, map> &mp) 23 | { 24 | if(root) 25 | { 26 | mp[d].push_back(root->data); 27 | 28 | if(root->left) 29 | Traverse(root->left,d+1,mp); 30 | 31 | if(root->right) 32 | Traverse(root->right,d,mp); 33 | } 34 | } 35 | vector diagonal(Node *root) 36 | { 37 | // your code here 38 | map >mp; 39 | 40 | Traverse(root,0,mp); 41 | vectorans; 42 | for(auto it : mp) 43 | { 44 | vectorv=it.second; 45 | 46 | for(auto itr: v) 47 | { 48 | ans.push_back(itr); 49 | } 50 | } 51 | 52 | return ans; 53 | } 54 | 55 | int main() 56 | { 57 | 58 | } -------------------------------------------------------------------------------- /Trees/Check_if_Trees_are_height_balanced.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // This is an input class. Do not edit. 6 | class BinaryTree { 7 | public: 8 | int value; 9 | int height; 10 | BinaryTree *left = nullptr; 11 | BinaryTree *right = nullptr; 12 | 13 | BinaryTree(int value) 14 | { 15 | this->value = value; 16 | height=0; 17 | } 18 | }; 19 | bool ans=true; 20 | int calHeight(BinaryTree *root) 21 | { 22 | if(root==NULL) 23 | return 0; 24 | 25 | if(root->left==NULL && root->right==NULL) 26 | { 27 | root->height=1; 28 | return 1; 29 | } 30 | int left=0,right=0; 31 | if(root->height==0) 32 | { 33 | left=calHeight(root->left); 34 | right=calHeight(root->right); 35 | 36 | if(abs(left-right)>1) 37 | { 38 | ans=false; 39 | } 40 | 41 | if(left>right) 42 | root->height=left+1; 43 | else 44 | root->height=right+1; 45 | } 46 | 47 | return root->height; 48 | } 49 | 50 | bool heightBalancedBinaryTree(BinaryTree *tree) { 51 | // Write your code here. 52 | ans=true; 53 | tree->height=calHeight(tree); 54 | return ans; 55 | } 56 | 57 | int main() 58 | { 59 | 60 | } -------------------------------------------------------------------------------- /Dynamic-Programming/Ballon_Burst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int Burst(int a[], int n) 6 | { 7 | int dp[n][n]; 8 | int i,j; 9 | for(i=0;i>n; 40 | n=n+2; 41 | int a[n]; 42 | a[0]=1; 43 | a[n-1]=1; 44 | cout<<"\n\nEnter the colours (nums) : "; 45 | for(i=1;i<=n-2;i++) 46 | { 47 | cin>>a[i]; 48 | } 49 | 50 | int score=Burst(a,n); 51 | cout<<"\n\nMax Score is : "< 4 | using namespace std; 5 | 6 | // x is the element to be searched in arr[0..n-1] 7 | // such that all elements differ by at-most k. 8 | int search(int arr[], int n, int x, int k) 9 | { 10 | // Traverse the given array starting from 11 | // leftmost element 12 | int i = 0; 13 | while (i < n) 14 | { 15 | // If x is found at index i 16 | if (arr[i] == x) 17 | return i; 18 | 19 | // Jump the difference between current 20 | // array element and x divided by k 21 | // We use max here to make sure that i 22 | // moves at-least one step ahead. 23 | i = i + max(1, abs(arr[i]-x)/k); 24 | } 25 | 26 | cout << "number is not present!"; 27 | return -1; 28 | } 29 | 30 | // Driver program to test above function 31 | int main() 32 | { 33 | int arr[] = {2, 4, 5, 7, 7, 6}; 34 | int x = 6; 35 | int k = 2; 36 | int n = sizeof(arr)/sizeof(arr[0]); 37 | cout << "Element " << x << " is present at index " 38 | << search(arr, n, x, k); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Stacks and Queues/Max_score_of_good_subarray_VIMP_NSR_NSL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | You are given an array of integers nums (0-indexed) and an integer k. 6 | 7 | The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). 8 | A good subarray is a subarray where i <= k <= j. 9 | 10 | Return the maximum possible score of a good subarray. 11 | */ 12 | 13 | // A greedy simple solution for this exits in greedy folder 14 | class Solution 15 | { 16 | public: 17 | int maximumScore(vector &nums, int k) 18 | { 19 | nums.push_back(0); 20 | stack st; 21 | int n = nums.size(), res = 0; 22 | for (int i = 0; i < n; i++) 23 | { 24 | while (!st.empty() && nums[st.top()] >= nums[i]) 25 | { 26 | int height = nums[st.top()]; 27 | st.pop(); 28 | int left = st.empty() ? -1 : st.top(); 29 | if (k < i && k > left) 30 | res = max(height * (i - left - 1), res); 31 | } 32 | st.push(i); 33 | } 34 | return res; 35 | } 36 | }; -------------------------------------------------------------------------------- /Trees/max_diff_between_node_and_ancestor_imp_rec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | // * Definition for a binary tree node. 7 | struct TreeNode { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode() : val(0), left(nullptr), right(nullptr) {} 12 | TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 13 | TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 14 | }; 15 | 16 | /* 17 | Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and 18 | a is an ancestor of b. 19 | 20 | A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b. 21 | 22 | 23 | */ 24 | class Solution { 25 | public: 26 | int maxAncestorDiff(TreeNode* root , int mn = 100000, int mx = 0) { 27 | if(!root) 28 | return mx-mn; 29 | 30 | mx = max(mx,root->val); 31 | mn = min(mn,root->val); 32 | 33 | return max(maxAncestorDiff(root->left,mn,mx) , maxAncestorDiff(root->right,mn,mx)); 34 | } 35 | }; -------------------------------------------------------------------------------- /Arrays/Sum_beauty_of_all_subarrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | /* 7 | 8 | Given an arr of length n, the beauty of any of its subarray is 9 | the number of elements in the subarray that occurs exactly once. 10 | 11 | Give sum of beauty of all possible subarrays 12 | 13 | */ 14 | long long calcBeauty(int n, vector arr) 15 | { 16 | // code here 17 | unordered_map> mp; 18 | 19 | for (int i = 0; i < n; i++) 20 | { 21 | int it = arr[i]; 22 | 23 | if (mp.find(it) == mp.end()) 24 | mp[it].push_back(-1); 25 | 26 | mp[it].push_back(i); 27 | } 28 | 29 | for (auto it : mp) 30 | { 31 | mp[it.first].push_back(n); 32 | } 33 | 34 | long long ans = 0; 35 | 36 | for (auto it : mp) 37 | { 38 | auto ls = it.second; 39 | 40 | for (int i = 1; i < ls.size() - 1; i++) 41 | { 42 | long long left = ls[i] - ls[i - 1]; 43 | long long right = ls[i + 1] - ls[i]; 44 | 45 | ans += (left * right); 46 | } 47 | } 48 | 49 | return ans; 50 | } 51 | int main() 52 | { 53 | } -------------------------------------------------------------------------------- /Arrays/merge_overlapping_intervals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | vector> merge(vector> &intervals) 12 | { 13 | 14 | sort(intervals.begin(), intervals.end()); 15 | 16 | stack> st; 17 | 18 | st.push(intervals[0]); 19 | 20 | for (int i = 1; i < intervals.size(); i++) 21 | { 22 | vector tp = st.top(); 23 | 24 | if (tp[1] < intervals[i][0]) 25 | st.push(intervals[i]); 26 | else 27 | { 28 | if (tp[1] < intervals[i][1]) 29 | { 30 | tp[1] = intervals[i][1]; 31 | } 32 | st.pop(); 33 | st.push(tp); 34 | } 35 | } 36 | 37 | vector> ans; 38 | 39 | while (!st.empty()) 40 | { 41 | ans.push_back(st.top()); 42 | st.pop(); 43 | } 44 | 45 | return ans; 46 | } 47 | }; 48 | 49 | int main() 50 | { 51 | } -------------------------------------------------------------------------------- /Dynamic-Programming/longest_palindromic_subsequence(Dynamic_programing).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | 5 | using namespace std; 6 | int lps(string s) 7 | { 8 | int i,j,len; 9 | int l=s.length(); 10 | int dp[l][l]; 11 | for(i=0;i>s; 45 | int l=s.length(); 46 | int ans=lps(s); //longest palindromic subsequence 47 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int rectangleMania(vector> coords) { 16 | // Write your code here. 17 | set< vector >st; 18 | int i,j,n; 19 | n=coords.size(); 20 | for(i=0;ix1 && y2>y1) || (x1>x2 && y1>y2) ) //Upper Right 36 | { 37 | if(st.find({x1,y2})!=st.end() && st.find({x2,y1})!=st.end() ) 38 | { 39 | count++; 40 | } 41 | } 42 | } 43 | } 44 | 45 | 46 | return (count); 47 | } 48 | 49 | 50 | int main() 51 | { 52 | 53 | } 54 | 55 | 56 | /* 57 | 58 | Given coords of points, return num f rectangkes that can be formed which are parallel with x and y axis 59 | */ -------------------------------------------------------------------------------- /Graphs/int1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int diff=INT_MAX; 8 | int n; 9 | /* 10 | for(i=0;i sum) 48 | { 49 | k--; 50 | } 51 | else 52 | { 53 | j++; 54 | } 55 | } 56 | 57 | } 58 | 59 | cout< 2 | 3 | using namespace std; 4 | 5 | 6 | int Bin_search(int a[], int key, int left, int right) 7 | { 8 | if(left>right) 9 | return -1; 10 | 11 | int mid=(left+right)/2; 12 | 13 | if(a[mid]==key) 14 | return mid; 15 | 16 | if(a[left]<=a[mid]) 17 | { 18 | if(a[left]<=key && key<=a[mid]) 19 | return Bin_search(a,key,left,mid-1); 20 | else 21 | return Bin_search(a,key,mid+1,right); 22 | } 23 | else 24 | { 25 | if(a[mid]<=key && key<=a[right]) 26 | return Bin_search(a,key,mid+1,right); 27 | else 28 | return Bin_search(a,key,left,mid-1); 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | int n,i; 35 | cout<<"Enter the num of elements in array : "; 36 | cin>>n; 37 | cout<<"\nEnter the Array Elements : "; 38 | int a[n]; 39 | for(i=0;i>a[i]; 43 | } 44 | 45 | cout<<"\nEnter the key to be searched : "; 46 | int key; 47 | cin>>key; 48 | 49 | int pos=Bin_search(a,key,0,n-1); 50 | 51 | cout<<"\nEle found at index : "< 2 | #include 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | 5 | using namespace std; 6 | int lps(string s) 7 | { 8 | int i,j,len; 9 | int l=s.length(); 10 | int dp[l][l]; 11 | for(i=0;i>s; 48 | int l=s.length(); 49 | int ans=lps(s); //longest palindromic subsequence 50 | cout< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int Kadens(vector &arr) 9 | { 10 | int n = arr.size(); 11 | 12 | int curr_max, globalMax = arr[0]; 13 | 14 | curr_max = arr[0]; 15 | 16 | for (int i = 1; i < n; i++) 17 | { 18 | curr_max = max(arr[i], curr_max + arr[i]); 19 | globalMax = max(globalMax, curr_max); 20 | } 21 | 22 | return globalMax; 23 | } 24 | 25 | int maximumSumRectangle(int R, int C, vector> M) 26 | { 27 | // code here 28 | 29 | int ans = INT_MIN; 30 | int x, y; 31 | 32 | for (int i = 0; i < C; i++) 33 | { 34 | vector arr(R, 0); 35 | 36 | for (int j = i; j < C; j++) 37 | { 38 | for (int k = 0; k < R; k++) 39 | { 40 | arr[k] += M[k][j]; 41 | } 42 | 43 | ans = max(ans, Kadens(arr)); 44 | } 45 | } 46 | 47 | // cout< 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node *left; 10 | Node *right; 11 | 12 | Node(int d=0) 13 | { 14 | data=d; 15 | left=NULL; 16 | right=NULL; 17 | } 18 | }; 19 | 20 | 21 | Node* FindLCA(Node *r, int m1, int m2) 22 | { 23 | if(r==NULL) 24 | return NULL; 25 | 26 | if(r->left!=NULL && r->right!=NULL) 27 | { 28 | if(r->left->data==m1 && r->right->data==m2) 29 | return r; 30 | } 31 | 32 | if(r->data >= m1 && r->data<= m2) 33 | return r; 34 | 35 | if(r->left) 36 | { 37 | if(m2 < r->data) 38 | return FindLCA(r->left,m1,m2); 39 | } 40 | 41 | if(r->right) 42 | { 43 | if(m1> r->data) 44 | return FindLCA(r->right,m1,m2); 45 | } 46 | 47 | 48 | } 49 | 50 | Node* LCA(Node *root, int n1, int n2) 51 | { 52 | //Your code here 53 | int m1=min(n1,n2); 54 | int m2=max(n1,n2); 55 | 56 | if(root==NULL) 57 | return NULL; 58 | 59 | Node *r=FindLCA(root,m1,m2); 60 | 61 | return r; 62 | } 63 | -------------------------------------------------------------------------------- /Divide and Conqure/Finding_peak_ele_2D_in_MLogN_time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | vector findPeakGrid(vector> &mat) 9 | { 10 | 11 | int startCol = 0, endCol = mat[0].size() - 1, midCol, row, maxRow; 12 | 13 | while (startCol <= endCol) 14 | { 15 | midCol = startCol + (endCol - startCol) / 2; 16 | 17 | row = 0; 18 | maxRow = 0; 19 | 20 | for (row = 0; row < mat.size(); row++) 21 | { 22 | maxRow = mat[row][midCol] > mat[maxRow][midCol] ? row : maxRow; 23 | } 24 | 25 | bool isrightBig = midCol + 1 <= endCol && mat[maxRow][midCol + 1] > mat[maxRow][midCol]; 26 | bool isleftBig = midCol - 1 >= startCol && mat[maxRow][midCol - 1] > mat[maxRow][midCol]; 27 | 28 | if (!isleftBig && !isrightBig) 29 | return vector{maxRow, midCol}; 30 | 31 | if (isrightBig) 32 | startCol = midCol + 1; 33 | else 34 | endCol = midCol - 1; 35 | } 36 | 37 | return vector{-1, -1}; 38 | } 39 | }; -------------------------------------------------------------------------------- /Searching and sorting/Aggresive_cows.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n,i,j,k; 9 | 10 | cout<<"Enter the num of Barns : "; 11 | cin>>n; 12 | cout<<"\nEnter the locations of Barns : \n"; 13 | int loc[n]; 14 | 15 | for(i=0;i>loc[i]; 17 | 18 | sort(loc,loc+n); 19 | 20 | cout<<"\nEnter num of cows : "; 21 | int c; 22 | cin>>c; 23 | 24 | int low=0, high=loc[n-1], mid, cnt, left; 25 | int best; 26 | mid=(low+high+1)/2; 27 | 28 | while(low<=high) 29 | { 30 | cnt=1; 31 | left=0; 32 | for(i=1;i=mid) 35 | { 36 | cnt++; 37 | left=i; 38 | 39 | if(cnt>=c) 40 | break; 41 | } 42 | } 43 | 44 | if(cnt>=c) 45 | { 46 | best=mid; 47 | low=mid+1; 48 | } 49 | else 50 | { 51 | high=mid-1; 52 | } 53 | 54 | } 55 | 56 | cout<<"\nMax of mini distance is : "< 2 | 3 | using namespace std; 4 | /* 5 | You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer 6 | fee representing a transaction fee. 7 | 8 | Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need 9 | to pay the transaction fee for each transaction. 10 | 11 | Note: You may not engage in multiple transactions simultaneously 12 | (i.e., you must sell the stock before you buy again). 13 | */ 14 | class Solution 15 | { 16 | public: 17 | int maxProfit(vector &prices, int fee) 18 | { 19 | 20 | int mini = prices[0]; 21 | 22 | int ans = 0; 23 | for (auto p : prices) 24 | { 25 | if (p < mini) 26 | mini = p; 27 | else if (p > mini + fee) 28 | { 29 | ans += (p - mini - fee); 30 | mini = p - fee; 31 | } 32 | } 33 | 34 | return ans; 35 | } 36 | }; 37 | 38 | //https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/201603/Python.-Greedy-is-good. -------------------------------------------------------------------------------- /Dynamic-Programming/coin_exchang_DYNAMIC.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 | 5 | using namespace std; 6 | long int exchange(long int a[],long int m,long int n) 7 | { 8 | int dp[n+1][m]; 9 | long int i,j,x,y; 10 | 11 | for(i=0;i=0)? dp[i-a[j]][j]:0; 23 | //not considering the jth denomination coin for amount i 24 | y=(j>=1)? dp[i][j-1]:0; 25 | 26 | dp[i][j]=x+y; 27 | } 28 | } 29 | 30 | return dp[n][m-1]; 31 | } 32 | 33 | int main() 34 | { 35 | long int n,i,m; 36 | cout<<"Enter the amount u want to exchange\n"; 37 | cin>>n; 38 | cout<<"Enter the number of denominations\n"; 39 | cin>>m; 40 | long int a[m]; 41 | cout<<"Enter the denominations(values) of coins\n"; 42 | for(i=0;i>a[i]; 45 | } 46 | long int ans=exchange(a,m,n); 47 | cout< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | bool possible(vector &nums, int mid, int k) 9 | { 10 | int j = 0; 11 | int count = 0; 12 | for (int i = 0; i < nums.size(); i++) 13 | { 14 | while (j < nums.size() && nums[j] - nums[i] <= mid) 15 | j++; 16 | 17 | j--; 18 | 19 | count += (j - i); 20 | } 21 | 22 | return count >= k; 23 | } 24 | 25 | int smallestDistancePair(vector &nums, int k) 26 | { 27 | 28 | sort(nums.begin(), nums.end()); 29 | 30 | int n = nums.size(); 31 | int low = 0, high = nums[n - 1] - nums[0], mid; 32 | 33 | int ans; 34 | while (low <= high) 35 | { 36 | mid = low + (high - low) / 2; 37 | 38 | if (possible(nums, mid, k)) 39 | { 40 | ans = mid; 41 | high = mid - 1; 42 | } 43 | else 44 | { 45 | low = mid + 1; 46 | } 47 | } 48 | 49 | return ans; 50 | } 51 | }; -------------------------------------------------------------------------------- /Strings/max_num_at_most_K_swaps_IMP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | Given a number K and string str of digits denoting a positive integer, 6 | build the largest number possible by performing swap operations on the digits of str at most K times. 7 | 8 | 9 | */ 10 | void findMax(int pos, int k, string &maxstr, string str) 11 | { 12 | if (k <= 0) 13 | return; 14 | 15 | int n = str.length(); 16 | char maxchar = str[pos]; 17 | for (int i = pos + 1; i < n; i++) 18 | { 19 | if (str[i] > maxchar) 20 | maxchar = str[i]; 21 | } 22 | 23 | if (maxchar != str[pos]) 24 | k--; 25 | 26 | for (int i = n - 1; i >= pos; i--) 27 | { 28 | if (str[i] == maxchar) 29 | { 30 | swap(str[i], str[pos]); 31 | 32 | if (str.compare(maxstr) > 0) 33 | maxstr = str; 34 | 35 | findMax(pos + 1, k, maxstr, str); 36 | 37 | swap(str[i], str[pos]); 38 | } 39 | } 40 | } 41 | string findMaximumNum(string str, int k) 42 | { 43 | // code here. 44 | 45 | string maxstr = str; 46 | findMax(0, k, maxstr, str); 47 | 48 | return maxstr; 49 | } -------------------------------------------------------------------------------- /Dynamic-Programming/DP_CP_K_OrderLCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int n, m, K; 6 | 7 | cin >> n >> m >> K; 8 | 9 | vector a(n), b(m); 10 | 11 | for (int i = 0; i < n; i++) 12 | cin >> a[i]; 13 | 14 | for (int i = 0; i < m; i++) 15 | cin >> b[i]; 16 | 17 | int dp[n + 1][m + 1][K + 1]; 18 | 19 | memset(dp, 0, sizeof dp); 20 | 21 | for (int k = 0; k <= K; k++) 22 | { 23 | for (int i = 0; i <= n; i++) 24 | { 25 | for (int j = 0; j <= m; j++) 26 | { 27 | if (i == 0 || j == 0) 28 | dp[i][j][k] = 0; 29 | else 30 | { 31 | if (a[i - 1] == b[j - 1]) 32 | dp[i][j][k] = dp[i - 1][j - 1][k] + 1; 33 | 34 | if (a[i - 1] != b[j - 1]) 35 | dp[i][j][k] = max(dp[i - 1][j][k], dp[i][j - 1][k]); 36 | 37 | if (k > 0) 38 | dp[i][j][k] = max(dp[i][j][k], 1 + dp[i - 1][j - 1][k - 1]); 39 | } 40 | } 41 | } 42 | } 43 | 44 | cout << dp[n][m][K]; 45 | } -------------------------------------------------------------------------------- /Arrays/two_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | long long int two_sum(vector&v, long long int s) 6 | { 7 | long long int i,j,count=0; 8 | 9 | unordered_mapmp; 10 | 11 | for(i=0;i>n; 46 | cin>>s; 47 | vectorv(n); 48 | 49 | for(i=0;i>v[i]; 52 | } 53 | 54 | long long int ns = two_sum(v,s); 55 | 56 | cout<<"\n\n"< 2 | 3 | using namespace std; 4 | 5 | /* 6 | 7 | The set [1, 2, 3, ..., n] contains a total of n! unique permutations. 8 | 9 | By listing and labeling all of the permutations in order, we get the following sequence for n = 3: 10 | 11 | "123" 12 | "132" 13 | "213" 14 | "231" 15 | "312" 16 | "321" 17 | Given n and k, return the kth permutation sequence. 18 | 19 | */ 20 | 21 | class Solution 22 | { 23 | public: 24 | string getPermutation(int n, int k) 25 | { 26 | 27 | int fact = 1; 28 | vector nums; 29 | for (int i = 1; i < n; i++) 30 | { 31 | fact = fact * i; 32 | nums.push_back(i); 33 | } 34 | 35 | nums.push_back(n); 36 | k = k - 1; 37 | string ans = ""; 38 | 39 | while (true) 40 | { 41 | ans = ans + to_string(nums[k / fact]); 42 | 43 | nums.erase(nums.begin() + k / fact); 44 | 45 | if (nums.size() == 0) 46 | break; 47 | 48 | k = k % fact; 49 | fact = fact / nums.size(); 50 | } 51 | 52 | return ans; 53 | } 54 | }; -------------------------------------------------------------------------------- /Back-Tracking and Recursion/Word_break_by_BackTracking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | vector ans; 7 | bool contains(vector &dict, string s, int n) 8 | { 9 | int i; 10 | for (i = 0; i < n; i++) 11 | { 12 | if (dict[i] == s) 13 | return true; 14 | } 15 | 16 | return false; 17 | } 18 | 19 | void wordBreakUntil(string s, int size, vector &dict, string res, int n) 20 | { 21 | int i, j; 22 | for (i = 1; i <= size; i++) 23 | { 24 | string prefix = s.substr(0, i); 25 | 26 | if (contains(dict, prefix, n)) 27 | { 28 | if (i == size) 29 | { 30 | res = res + prefix; 31 | 32 | ans.push_back(res); 33 | return; 34 | } 35 | 36 | wordBreakUntil(s.substr(i, size - i), size - i, dict, res + prefix + " ", n); 37 | } 38 | } 39 | } 40 | vector wordBreak(int n, vector &dict, string s) 41 | { 42 | // code here 43 | 44 | string res = ""; 45 | 46 | wordBreakUntil(s, s.size(), dict, res, n); 47 | 48 | return ans; 49 | } 50 | 51 | int main() 52 | { 53 | } -------------------------------------------------------------------------------- /Graphs/component_detection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int get_comp(vector> &adjlst, int visited[], int curr) 7 | { 8 | if (visited[curr]) 9 | return 0; 10 | 11 | visited[curr] = 1; 12 | int ans = 1; 13 | for (auto i : adjlst[curr]) 14 | { 15 | if (visited[i] == 0) 16 | { 17 | 18 | ans += get_comp(adjlst, visited, i); 19 | } 20 | } 21 | 22 | return ans; 23 | } 24 | 25 | int main() 26 | { 27 | int i, j, v, e, x, y; 28 | 29 | cout << "Enter the num of Vertices and Edges : "; 30 | cin >> v >> e; 31 | 32 | vector> adjlst(v); 33 | 34 | cout << "\nEnter the Edges : "; 35 | 36 | for (i = 0; i < e; i++) 37 | { 38 | cin >> x >> y; 39 | 40 | // x -- y 41 | adjlst[x].push_back(y); 42 | adjlst[y].push_back(x); 43 | } 44 | 45 | int visited[v] = {0}; 46 | vector components; 47 | 48 | for (i = 0; i < v; i++) 49 | { 50 | if (visited[i] == 0) 51 | { 52 | int size = get_comp(adjlst, visited, i); 53 | components.push_back(size); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /Arrays/min_jumps_to_end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | // Function to return minimum number of jumps to end of array 8 | int minJumps(int arr[], int n){ 9 | // Your code here 10 | int *jump; 11 | int i,j; 12 | jump=new int [n]; 13 | jump[0]=0; 14 | if(n==0 || arr[0]==0) 15 | { 16 | return -1; 17 | } 18 | 19 | for(i=1;i>t; 45 | while(t--) 46 | { 47 | int n,i,j; 48 | cin>>n; 49 | int arr[n]; 50 | for(int i=0; i>arr[i]; 52 | 53 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int dp[5001][5001]; 8 | //The crux of this problles is that when u define the dp 2d array inside the function, it fails for few cases, but when declared global 9 | //it passes this beciz - Instead of defining 2d matrix inside a function, define it globally. 10 | // In this way it will acquire data segment instead of stack memory. 11 | int commonChild(string s1, string s2) 12 | { 13 | int i,j,n,m; 14 | n=s1.length(); 15 | m=s2.length(); 16 | 17 | //int dp[n+1][m+1]; 18 | 19 | for(i=0;i<=n;i++) 20 | { 21 | for(j=0;j<=m;j++) 22 | { 23 | if(i==0 || j==0) 24 | dp[i][j]=0; 25 | else 26 | { 27 | if(s1[i-1]==s2[j-1]) 28 | dp[i][j]=1+dp[i-1][j-1]; 29 | else 30 | dp[i][j]=max(dp[i][j-1],dp[i-1][j]); 31 | } 32 | } 33 | } 34 | 35 | return dp[n][m]; 36 | } 37 | int main() 38 | { 39 | string s1,s2; 40 | cin>>s1; 41 | cin>>s2; 42 | 43 | int count = commonChild(s1,s2); 44 | 45 | cout<<"\n\n*****\n\n"< 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int numberOfArithmeticSlices(vector &nums) 9 | { 10 | 11 | int n = nums.size(); 12 | 13 | if (n < 3) 14 | return 0; 15 | 16 | int curr_len = 0, curr_diff = nums[1] - nums[0], last_diff; 17 | int ans = 0; 18 | for (int i = 1; i < n - 1; i++) 19 | { 20 | last_diff = nums[i + 1] - nums[i]; 21 | 22 | if (last_diff == curr_diff) 23 | curr_len++; 24 | else 25 | { 26 | curr_len = 0; 27 | curr_diff = last_diff; 28 | } 29 | 30 | ans += curr_len; 31 | } 32 | 33 | return ans; 34 | } 35 | }; 36 | 37 | /* 38 | An integer array is called arithmetic if it consists of at least three elements and if the difference between any two 39 | consecutive elements is the same. 40 | 41 | For example, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences. 42 | Given an integer array nums, return the number of arithmetic subarrays of nums. 43 | 44 | A subarray is a contiguous subsequence of the array. 45 | */ -------------------------------------------------------------------------------- /Heaps/Reorganize_strings_leetcode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | string reorganizeString(string s) 6 | { 7 | 8 | unordered_map mp; 9 | 10 | int i, j, n; 11 | n = s.length(); 12 | 13 | for (i = 0; i < n; i++) 14 | { 15 | mp[s[i]]++; 16 | } 17 | 18 | priority_queue, vector>> pq; 19 | 20 | for (auto it = mp.begin(); it != mp.end(); it++) 21 | { 22 | pq.push({it->second, it->first}); 23 | } 24 | 25 | string ans = ""; 26 | 27 | while (!pq.empty()) 28 | { 29 | pair p = pq.top(); 30 | pq.pop(); 31 | 32 | if (pq.empty()) 33 | { 34 | if (p.first > 1) 35 | return ""; 36 | else 37 | return (ans + p.second); 38 | } 39 | 40 | pair q = pq.top(); 41 | pq.pop(); 42 | 43 | ans = ans + p.second + q.second; 44 | 45 | if (p.first > 1) 46 | pq.push({p.first - 1, p.second}); 47 | 48 | if (q.first > 1) 49 | pq.push({q.first - 1, q.second}); 50 | } 51 | 52 | return ans; 53 | } 54 | 55 | int main() 56 | { 57 | } -------------------------------------------------------------------------------- /Back-Tracking and Recursion/Generate_all_possible_nums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], 6 | the task is to list all words which are possible by pressing these numbers. 7 | */ 8 | void Generate(int a[], int pos, int N, string &s, vector &ans, unordered_map mp) 9 | { 10 | if (pos >= N) 11 | { 12 | ans.push_back(s); 13 | return; 14 | } 15 | 16 | string curr = mp[a[pos]]; 17 | 18 | int len = curr.length(); 19 | for (int i = 0; i < len; i++) 20 | { 21 | s.push_back(curr[i]); 22 | Generate(a, pos + 1, N, s, ans, mp); 23 | s.pop_back(); 24 | } 25 | 26 | return; 27 | } 28 | vector possibleWords(int a[], int N) 29 | { 30 | //Your code here 31 | unordered_map mp; 32 | 33 | mp[2] = "abc"; 34 | mp[3] = "def"; 35 | mp[4] = "ghi"; 36 | mp[5] = "jkl"; 37 | mp[6] = "mno"; 38 | mp[7] = "pqrs"; 39 | mp[8] = "tuv"; 40 | mp[9] = "wxyz"; 41 | 42 | vector ans; 43 | 44 | string s = ""; 45 | Generate(a, 0, N, s, ans, mp); 46 | 47 | return ans; 48 | } -------------------------------------------------------------------------------- /Arrays/kth_smallest_in_arr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | // arr : given array 5 | // l : starting index of the array i.e 0 6 | // r : ending index of the array i.e size-1 7 | // k : find kth smallest element and return using this function 8 | 9 | using namespace std; 10 | 11 | int kthSmallest(int arr[], int l, int r, int k) 12 | { 13 | //code here 14 | 15 | //Heap approach 16 | /* 17 | priority_queue,greater>minh; 18 | 19 | for(int i=l;i<=r;i++) 20 | { 21 | minh.push(arr[i]); 22 | } 23 | 24 | int count=0; 25 | 26 | int ans; 27 | while(count s(arr, arr + n); 40 | set::iterator itr = s.begin(); // s.begin() returns a pointer to first 41 | // element in the set 42 | advance(itr, k - 1); // itr points to kth element in set 43 | 44 | return (*itr); 45 | } -------------------------------------------------------------------------------- /Searching and sorting/Nth_magical_num_Bin_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | /* 5 | A positive integer is magical if it is divisible by either a or b. 6 | 7 | Given the three integers n, a, and b, return the nth magical number. Since the answer may be very large, return it modulo 109 + 7. 8 | 9 | 10 | */ 11 | class Solution 12 | { 13 | public: 14 | int nthMagicalNumber(int n, int a, int b) 15 | { 16 | 17 | long long int low, high, mid, ans, mod; 18 | 19 | mod = 1e9 + 7; 20 | 21 | if (b < a) 22 | swap(a, b); 23 | 24 | low = a; 25 | high = (long long)a * (long long)n; 26 | 27 | long long int lcm = ((long long)a * (long long)b) / __gcd(a, b); 28 | 29 | long long int cnt; 30 | 31 | while (low <= high) 32 | { 33 | mid = (low + high) >> 1; 34 | 35 | cnt = (mid / a + mid / b - mid / lcm); 36 | 37 | if (cnt >= n) 38 | { 39 | ans = mid % mod; 40 | high = mid - 1; 41 | } 42 | else 43 | { 44 | low = mid + 1; 45 | } 46 | } 47 | 48 | return (int)ans; 49 | } 50 | }; --------------------------------------------------------------------------------