├── CSES └── Introductory Problems │ ├── 1068 │ ├── 1083 │ ├── 1068.cpp │ ├── 1083.cpp │ ├── input.txt │ └── output.txt ├── Competitive Programming Handbook ├── Competitive Programming Handbook.pdf └── cphb │ ├── README.md │ ├── book.pdf │ ├── book.tex │ ├── chapter01.tex │ ├── chapter02.tex │ ├── chapter03.tex │ ├── chapter04.tex │ ├── chapter05.tex │ ├── chapter06.tex │ ├── chapter07.tex │ ├── chapter08.tex │ ├── chapter09.tex │ ├── chapter10.tex │ ├── chapter11.tex │ ├── chapter12.tex │ ├── chapter13.tex │ ├── chapter14.tex │ ├── chapter15.tex │ ├── chapter16.tex │ ├── chapter17.tex │ ├── chapter18.tex │ ├── chapter19.tex │ ├── chapter20.tex │ ├── chapter21.tex │ ├── chapter22.tex │ ├── chapter23.tex │ ├── chapter24.tex │ ├── chapter25.tex │ ├── chapter26.tex │ ├── chapter27.tex │ ├── chapter28.tex │ ├── chapter29.tex │ ├── chapter30.tex │ ├── list.tex │ └── preface.tex ├── Part-I Basic Technieques ├── Chapter 1 (Introduction) │ ├── 1_code │ ├── 1_code.cpp │ ├── 2_fast │ ├── 2_fast.cpp │ ├── 3_read_unlimited │ ├── 3_read_unlimited.cpp │ ├── 4_read_input_output_from_files │ ├── 4_read_input_output_from_files.cpp │ ├── 5_short_code │ ├── 5_short_code.cpp │ ├── 6_mathematicalFunctions │ ├── 6_mathematicalFunctions.cpp │ ├── input.txt │ └── output.txt ├── Chapter 10 (Bit Manipulation) │ ├── 10_counting_subsets │ ├── 10_counting_subsets.cpp │ ├── 1_basic_bit_intro │ ├── 1_basic_bit_intro.cpp │ ├── 2_bit_operations │ ├── 2_bit_operations.cpp │ ├── 3_integer_to_number │ ├── 3_integer_to_number.cpp │ ├── 4_built_in_c++_bitmanuplation_function │ ├── 4_built_in_c++_bitmanuplation_function.cpp │ ├── 5_representing_sets_bit_manuplation │ ├── 5_representing_sets_bit_manuplation.cpp │ ├── 6_hamming_distance │ ├── 6_hamming_distance.cpp │ ├── 7_counting_subgrids │ ├── 7_counting_subgrids.cpp │ ├── 8_product_selection_optimal │ ├── 8_product_selection_optimal.cpp │ ├── 9_elevator_rides_optimal │ ├── 9_elevator_rides_optimal.cpp │ ├── input.txt │ └── output.txt ├── Chapter 2 (Time Complexity) │ ├── 1_time_complexity │ ├── 1_time_complexity.cpp │ ├── 2_timecomplexity_time │ ├── 2_timecomplexity_time.cpp │ ├── 3_maximum_sum_subarray │ ├── 3_maximum_sum_subarray.cpp │ ├── intput.txt │ ├── output.txt │ ├── test │ └── test.cpp ├── Chapter 3 (Sorting) │ ├── 1_sorting │ ├── 1_sorting.cpp │ ├── 2_stl_sorting │ ├── 2_stl_sorting.cpp │ ├── 3_vector_sort_reverse_compare │ ├── 3_vector_sort_reverse_compare.cpp │ ├── 4_binary_search │ ├── 4_binary_search.cpp │ ├── 5_binary_search_efficient │ ├── 5_binary_search_efficient.cpp │ ├── 6_array_bound_functions │ ├── 6_array_bound_functions.cpp │ ├── 7_maximum_minimum │ ├── 7_maximum_minimum.cpp │ ├── input.txt │ └── output.txt ├── Chapter 4 (Data Structures) │ ├── 10_policy_based_data_structures │ ├── 10_policy_based_data_structures.cpp │ ├── 1_vector_stl │ ├── 1_vector_stl.cpp │ ├── 2_set │ ├── 2_set.cpp │ ├── 3_map_ordered_unordered │ ├── 3_map_ordered_unordered.cpp │ ├── 4_iterators_ranges │ ├── 4_iterators_ranges.cpp │ ├── 5_bitset │ ├── 5_bitset.cpp │ ├── 6_deque │ ├── 6_deque.cpp │ ├── 7_stack │ ├── 7_stack.cpp │ ├── 8_queue │ ├── 8_queue.cpp │ ├── 9_priority_queue_min_max │ ├── 9_priority_queue_min_max.cpp │ ├── input.txt │ ├── output.txt │ └── output.xt ├── Chapter 5 (Complete Search) │ ├── 1_generating_subset │ ├── 1_generating_subset.cpp │ ├── 2_generating_subset_powerset │ ├── 2_generating_subset_powerset.cpp │ ├── 3_generating_permutations │ ├── 3_generating_permutations.cpp │ ├── 4_generating_permutations_with_stl │ ├── 4_generating_permutations_with_stl.cpp │ ├── 5_backtracking_queen_placement_not_queen_kills │ ├── 5_backtracking_queen_placement_not_queen_kills.cpp │ ├── 6_N*N_Board_left_corner_to_bottom_corner_path_visit_each_cell │ ├── 6_N*N_Board_left_corner_to_bottom_corner_path_visit_each_cell.cpp │ ├── 7_meet_in_middle │ ├── 7_meet_in_middle.cpp │ ├── input.txt │ ├── output.txt │ └── queenMovesOnChessBoard.gif ├── Chapter 6 (Greedy Algorithms) │ ├── 1_coin_change │ ├── 1_coin_change.cpp │ ├── 2_scheduling_start_finish │ ├── 2_scheduling_start_finish.cpp │ ├── 3_tasks_and_deadline │ ├── 3_tasks_and_deadline.cpp │ ├── 4_minimizing_sums │ ├── 4_minimizing_sums.cpp │ ├── 5_huffman_coding │ ├── 5_huffman_coding.cpp │ ├── input.txt │ ├── lect07-greedy-sched.pdf │ └── output.txt ├── Chapter 7 (Dyanmic Programming) │ ├── 1_coin_problem_dp │ ├── 1_coin_problem_dp.cpp │ ├── 2_coin_problem_counting_no_of_solution │ ├── 2_coin_problem_counting_no_of_solution.cpp │ ├── 3_longest_increasing_subsequence │ ├── 3_longest_increasing_subsequence.cpp │ ├── 4_paths_in_grid │ ├── 4_paths_in_grid.cpp │ ├── 5_knapsack_problem │ ├── 5_knapsack_problem.cpp │ ├── 6_edit_distance │ ├── 6_edit_distance.cpp │ ├── 7_couting_tilings │ ├── 7_couting_tilings.cpp │ ├── 7_couting_tilings.o │ ├── domino-tilings.pdf │ ├── input.txt │ ├── output.txt │ └── test ├── Chapter 8 (Amortized Analysis) │ ├── 1_subarraySum.cpp │ ├── 2_2SUM_Problem_Two_Pointers │ ├── 2_2SUM_Problem_Two_Pointers.cpp │ ├── 3_Nearest_Smallest_Element │ ├── 3_Nearest_Smallest_Element.cpp │ ├── 4_sliding_window_minimum │ ├── 4_sliding_window_minimum.cpp │ ├── input.txt │ ├── output.txt │ └── subarraySum └── Chapter 9 (Range Queries) │ ├── 1_range_sum │ ├── 1_range_sum.cpp │ ├── 2_static_array_sum_queries_prefix_sum │ ├── 2_static_array_sum_queries_prefix_sum.cpp │ ├── 3_minimum_queries_sparse_table │ ├── 3_minimum_queries_sparse_table.cpp │ ├── 4_Fenwick_Tree_or_Binary_Indexed_Tree │ ├── 4_Fenwick_Tree_or_Binary_Indexed_Tree.cpp │ ├── 5_segment_tree │ ├── 5_segment_tree.cpp │ ├── input.txt │ └── output.txt ├── Part-II Graph Algorithms ├── Chapter 11 (Basics of graphs) │ ├── 1_adjacency_list │ ├── 1_adjacency_list.cpp │ ├── 2_adjacency_matrix │ ├── 2_adjacency_matrix.cpp │ ├── 3_edge_list │ ├── 3_edge_list.cpp │ ├── input.txt │ └── output.txt ├── Chapter 12 (Graph traversal) │ ├── 1_depth_first_search_dfs │ ├── 1_depth_first_search_dfs.cpp │ ├── 2_breadth_first_search │ ├── 2_breadth_first_search.cpp │ ├── 3_connectivity_check │ ├── 3_connectivity_check.cpp │ ├── 4_finding_cycles │ ├── 4_finding_cycles.cpp │ ├── 5_bipartiteness_check │ ├── 5_bipartiteness_check.cpp │ ├── input.txt │ └── output.txt ├── Chapter 13 (Shortest paths) │ ├── 1_bellman_ford_algorithm │ ├── 1_bellman_ford_algorithm.cpp │ ├── 2_negative_cycle │ ├── 2_negative_cycle.cpp │ ├── 3_shortest_path_faster_algorithm_spfa_efficient_bellman_ford_algorithm │ ├── 3_shortest_path_faster_algorithm_spfa_efficient_bellman_ford_algorithm.cpp │ ├── 4_dijkstra_algorithm │ ├── 4_dijkstra_algorithm.cpp │ ├── 5_floyd_warshall_algorithm_shortest_path_first_algorithm_graph │ ├── 5_floyd_warshall_algorithm_shortest_path_first_algorithm_graph.cpp │ ├── input.txt │ └── output.txt ├── Chapter 14 (Tree algorithms) │ ├── 1_tree_traversal │ ├── 1_tree_traversal.cpp │ ├── 2_no_of_nodes_in_subtree_using_DP │ ├── 2_no_of_nodes_in_subtree_using_DP.cpp │ ├── 3_tree_diameter │ ├── 3_tree_diameter.cpp │ ├── 4_tree_diameter_using_DP │ ├── 4_tree_diameter_using_DP.cpp │ ├── 5_all_longest_path │ ├── 5_all_longest_path.cpp │ ├── 6_binary_tree_traversal_inorder_preorder_postorder_traversals │ ├── 6_binary_tree_traversal_inorder_preorder_postorder_traversals.cpp │ ├── input.txt │ ├── output.txt │ └── output.xt ├── Chapter 15 (Spanning trees) │ ├── 1_kruskal_minimal_spanning_algorithm │ ├── 1_kruskal_minimal_spanning_algorithm.cpp │ ├── 2_prims_minimal_spanning_tree_algorithm │ ├── 2_prims_minimal_spanning_tree_algorithm.cpp │ ├── input.txt │ └── output.txt ├── Chapter 16 (Directed graphs) │ ├── 1_topological_sorting_directed_acyclic_graph │ ├── 1_topological_sorting_directed_acyclic_graph.cpp │ ├── 2_counting_the_number_of_paths │ ├── 2_counting_the_number_of_paths.cpp │ ├── 3_extending_dijkstra_algorithm │ ├── 3_extending_dijkstra_algorithm.cpp │ ├── 4_successor_paths │ ├── 4_successor_paths.cpp │ ├── 5_floyds_algorithm_cycle_detection │ ├── 5_floyds_algorithm_cycle_detection.cpp │ ├── input.txt │ └── output.txt ├── Chapter 17 (Strong connectivity) │ ├── 1_kosaraju_s_algorithm │ ├── 1_kosaraju_s_algorithm.cpp │ ├── 2_2SAT_problem │ ├── 2_2SAT_problem.cpp │ ├── 2_2SAT_problem.cpp~ │ ├── 2_2SAT_problem.o │ ├── input.txt │ ├── input.txt~ │ ├── output.txt │ ├── output.txt~ │ └── test ├── Chapter 18 (Tree queries) │ ├── 1_Finding_Ancestors_Tree │ ├── 1_Finding_Ancestors_Tree.cpp │ ├── 1_Finding_Ancestors_Tree.cpp~ │ ├── 2_Subtree_Queries_Tree │ ├── 2_Subtree_Queries_Tree.cpp │ ├── 2_Subtree_Queries_Tree.cpp~ │ ├── 3_Segment_Tree_Sum_Array │ ├── 3_Segment_Tree_Sum_Array.cpp │ ├── 3_Segment_Tree_Sum_Array.cpp~ │ ├── 4_Path_Query_NARRAY_TREE_SEGMENT_TREE │ ├── 4_Path_Query_NARRAY_TREE_SEGMENT_TREE.cpp │ ├── 4_Path_Query_NARRAY_TREE_SEGMENT_TREE.cpp~ │ ├── 5_Lowest_Common_Ancestor_LCA_Tree │ ├── 5_Lowest_Common_Ancestor_LCA_Tree.cpp │ ├── 5_Lowest_Common_Ancestor_LCA_Tree.cpp~ │ ├── 6_Mergin_Data_Structures │ ├── 6_Mergin_Data_Structures.cpp │ ├── 6_Mergin_Data_Structures.cpp~ │ ├── 7_Tarjans_Algorithm_LCA_DSU_Offline_Algorithms │ ├── 7_Tarjans_Algorithm_LCA_DSU_Offline_Algorithms.cpp │ ├── 7_Tarjans_Algorithm_LCA_DSU_Offline_Algorithms.cpp~ │ ├── input.txt │ ├── input.txt~ │ ├── output.txt │ └── output.txt~ ├── Chapter 19 (Paths and circuits) │ ├── 1_Eulerian_Path_Circuits_Graph │ ├── 1_Eulerian_Path_Circuits_Graph.cpp │ ├── 1_Eulerian_Path_Circuits_Graph.cpp~ │ ├── 2_Hamiltonain_Path_Circuit_Graph_Theory │ ├── 2_Hamiltonain_Path_Circuit_Graph_Theory.cpp │ ├── 2_Hamiltonain_Path_Circuit_Graph_Theory.cpp~ │ ├── 3_De_Bruijn_Sequences_Graph │ ├── 3_De_Bruijn_Sequences_Graph.cpp │ ├── 3_De_Bruijn_Sequences_Graph.cpp~ │ ├── 4_Knights_Tours_Backtracking │ ├── 4_Knights_Tours_Backtracking.cpp │ ├── 4_Knights_Tours_Backtracking.cpp~ │ ├── 5_Warnsdorffs_Algorithm_Knights_Tour │ ├── 5_Warnsdorffs_Algorithm_Knights_Tour.cpp │ ├── 5_Warnsdorffs_Algorithm_Knights_Tour.cpp~ │ ├── input.txt │ ├── input.txt~ │ ├── knightsMoveOnChessBoard │ └── output.txt └── Chapter 20 (Flows and cuts) │ ├── 1_Ford_Fulkerson_Algorithm_Edmonds_Karp_Algorithm_BFS │ ├── 1_Ford_Fulkerson_Algorithm_Edmonds_Karp_Algorithm_BFS.cpp │ ├── 1_Ford_Fulkerson_Algorithm_Edmonds_Karp_Algorithm_BFS.cpp~ │ ├── 2_Maximum_Cut_Graph.cpp │ ├── 2_Maximum_Cut_Graph.cpp~ │ ├── 3_Maximum_Number_Edges_Disjoint_Path_Graph.cpp │ ├── 3_Maximum_Number_Edges_Disjoint_Path_Graph.cpp~ │ ├── 4_Maximum_Matching_Bipartile_Graph.cpp │ ├── 5_Path_Cover_Algorithm.cpp │ ├── 5_Path_Cover_Algorithm.cpp~ │ ├── input.txt │ ├── input.txt~ │ ├── output.txt │ └── output.txt~ ├── Part-III Advanced Topics ├── Chapter 21 (Number Theory) │ ├── .gitattributes │ ├── 1_Primes_and_Fators_Seive_Eratosthenes_Euclid_Algorithm_GCD.cpp │ ├── 1_Primes_and_Fators_Seive_Eratosthenes_Euclid_Algorithm_GCD.cpp~ │ ├── 1_Primes_and_Fators_Seive_Eratosthenes_Euclid_Algorithm_GCD.o │ ├── 2_Modular_Arithmetic.cpp │ ├── 2_Modular_Arithmetic.cpp~ │ ├── 2_Modular_Arithmetic.o │ ├── input.txt │ ├── input.txt~ │ ├── output.txt │ ├── output.txt~ │ └── test ├── Chapter 22 (Combinatorics) │ ├── 1_Binomial_Coefficients │ ├── 1_Binomial_Coefficients.cpp │ ├── 1_Binomial_Coefficients.cpp~ │ ├── 1_Binomial_Coefficients.o │ ├── 2_Catalan_Number.cpp │ ├── 2_Catalan_Number.cpp~ │ ├── 2_Catalan_Number.o │ ├── input.txt │ ├── input.txt~ │ ├── ls │ ├── output.txt │ ├── output.txt~ │ ├── output.xt │ └── test ├── Chapter 23 (Matrices) │ ├── 1_Matrix_Operations.cpp │ ├── 1_Matrix_Operations.cpp~ │ ├── 1_Matrix_Operations.o │ ├── input.txt │ ├── input.txt~ │ ├── output.txt │ └── test ├── Chapter 24 (Probability) │ ├── 1_Basic_Probability.cpp │ ├── 1_Basic_Probability.cpp~ │ ├── 1_Basic_Probability.o │ ├── input.txt │ ├── input.txt~ │ ├── output.txt │ └── test ├── Chapter 25 (Game Theory) │ ├── 1_GameState.cpp │ ├── 1_GameState.cpp~ │ ├── 1_GameState.o │ ├── input.txt │ ├── input.txt~ │ ├── output.txt │ └── test ├── Chapter 26 (String Algorithms) │ ├── input.txt │ └── output.txt ├── Chapter 27 (Square Root Algorithms) │ ├── input.txt │ └── output.txt ├── Chapter 28 (Segment Trees Revised) │ ├── input.txt │ └── output.txt ├── Chapter 29 (Geometry) │ ├── input.txt │ └── output.txt └── Chapter 30 (Sweep Line Algorithms) │ ├── input.txt │ └── output.txt ├── README.md ├── README.md~ └── hritikkumar-cp.geany /CSES/Introductory Problems/1068: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/CSES/Introductory Problems/1068 -------------------------------------------------------------------------------- /CSES/Introductory Problems/1068.cpp: -------------------------------------------------------------------------------- 1 | 2 | // @hritikkumar 3 | 4 | #include // all header files 5 | 6 | #define endl '\n' 7 | 8 | using namespace std; //namespace created as std 9 | 10 | int main(void) 11 | { 12 | std::ios::sync_with_stdio(false); cin.tie(0); 13 | /*#ifndef ONLINE_JUDGE 14 | freopen("input.txt", "r", stdin); 15 | freopen("output.txt", "w", stdout); 16 | #endif 17 | */ 18 | long long int n; 19 | cin>>n; 20 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); cin.tie(0); 12 | /*#ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif*/ 16 | 17 | int n; 18 | cin>>n; 19 | int* num = new int [n-1]; 20 | for(int i=0;i>num[i]; 22 | 23 | int ans = 0; 24 | for(int i=0;i // all header files 4 | 5 | using namespace std; //namespace created as std 6 | 7 | int main(void) // main function return type is int and input parameter is void (empty) 8 | { 9 | // solution comes here 10 | 11 | return 0; //return type is int 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 1 (Introduction)/2_fast: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 1 (Introduction)/2_fast -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 1 (Introduction)/2_fast.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | #include // all header files 3 | using namespace std; //namespace created as std 4 | 5 | int main(void) 6 | { 7 | ios::sync_with_stdio(0); 8 | cin.tie(0); 9 | // makes the execution faster 10 | 11 | return 0; //return type is int 12 | } 13 | // speed ups the program 14 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 1 (Introduction)/3_read_unlimited: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 1 (Introduction)/3_read_unlimited -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 1 (Introduction)/3_read_unlimited.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | #include // all header files 3 | using namespace std; //namespace created as std 4 | 5 | int main(void) 6 | { 7 | int x; // int type varable 8 | // int can store up to 10^5 values 9 | while(cin>>x) // unlimited input in x 10 | { 11 | cout<<"X: "< // all header files 3 | using namespace std; //namespace created as std 4 | 5 | int main(void) 6 | { 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | int t; // test cases 12 | cin>>t; 13 | string tmp; 14 | getline(cin, tmp); // additional line for test cases because that line should be ened 15 | while(t--) 16 | { 17 | string s; 18 | getline(cin, s); 19 | cout<<"S: "< // all header files 3 | using namespace std; //namespace created as std 4 | 5 | // typedefing of the data types 6 | typedef long long ll; 7 | typedef vector vi; 8 | typedef pair pi; 9 | 10 | 11 | // Macros 12 | #define F first 13 | #define S second 14 | #define PB push_back 15 | #define MP make_pair 16 | 17 | int main(void) 18 | { 19 | #ifndef ONLINE_JUDGE 20 | freopen("input.txt", "r", stdin); 21 | freopen("output.txt", "w", stdout); 22 | #endif 23 | 24 | long long int a = 1e-9; // a = 10^-9 25 | double x = 0.3*0.3 + 1; 26 | printf("%d\n%.20f\n", a,x); // floating point precision 27 | 28 | ll val = 1e+18; 29 | printf("%d\n", val); 30 | vector vpi; 31 | vpi.PB(MP(a, val)); 32 | int d = vpi[0].F + vpi[0].S; 33 | return 0; //return type is int 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 1 (Introduction)/6_mathematicalFunctions: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 1 (Introduction)/6_mathematicalFunctions -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 1 (Introduction)/6_mathematicalFunctions.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | #include // all header files 3 | using namespace std; 4 | 5 | int main(void) 6 | { 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | int n; 12 | cout<<"Sum of N natuaral numbers: N? : "; 13 | cin>>n; 14 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector countingSubsetSum(vector &, int ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int n; 19 | cin>>n; 20 | vector value(1<>value[i]; 22 | vector ans = countingSubsetSum(value, n); 23 | for(int i=0;i<(signed)ans.size();i++) 24 | { 25 | cout<<"{"; 26 | for(int j=0;j countingSubsetSum(vector &value, int n) 37 | { 38 | vector sum(1< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); // fastio 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | int num; 17 | cin>>num; 18 | unsigned int unsigned_num = num; 19 | cout<>val; 22 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); // fastio 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | int num1, num2; 18 | cin>>num1>>num2; 19 | int AND = num1 & num2, OR = num1 | num2, XOR = num1 ^ num2; 20 | cout<>x; 29 | int left_shift = x<<1, right_shift = x>>1; 30 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | string intToBinary(int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt","r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int num; 20 | cin>>num; // integer to number 21 | string binary = intToBinary(num); 22 | cout<>k; // kth bit (0-based from right to left) 25 | int num1 = num | (1<=0;i--) 63 | { 64 | if( num&(1< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); // fastio 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | /* 18 | • __builtin_clz ( x ): the number of zeros at the beginning of the number 19 | • __builtin_ctz ( x ): the number of zeros at the end of the number 20 | • __builtin_popcount ( x ): the number of ones in the number 21 | • __builtin_parity ( x ): the parity (even or odd) of the number of ones 22 | */ 23 | 24 | int num; 25 | cin>>num; 26 | cout<< __builtin_clz(num)< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); // fastio 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r",stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | /* 17 | set syntax bit syntax 18 | intersection a ∩ b a & b 19 | union a ∪ b a | b 20 | complement ā ~ a 21 | difference a \ b a & (~ b ) 22 | */ 23 | 24 | int x = 0; 25 | x |= (1<<1); 26 | x |= (1<<3); 27 | x |= (1<<4); 28 | x |= (1<<8); 29 | cout<<__builtin_popcount(x)<>n; 40 | vector a(n); 41 | for(int i=0;i>a[i]; 42 | int k; 43 | cin>>k; 44 | // print all the subset size of exactly k 45 | for(int b = 0;b<(1<>x; 59 | int b = 0; 60 | do{ 61 | for(int i=0;i // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int hammingDistance(string, string); 10 | 11 | int hammingDistance(int , int ); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); // fastio 16 | #ifndef ONLINE_JUDGE 17 | freopen("input.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | 21 | /*The Hamming distance hamming ( a, b ) between two strings a and b of equal 22 | length is the number of positions where the strings differ. For example, 23 | hamming (01101, 11001) = 2. 24 | */ 25 | 26 | string a, b; 27 | cin>>a>>b; 28 | int ans = hammingDistance(a, b); 29 | cout<>num1>>num2; 33 | ans = hammingDistance(num1, num2); 34 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int countingSubgrids(vector> &); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | vector> color(n, vector(n)); 22 | for(int i=0;i>color[i][j]; 23 | int ans = countingSubgrids(color); 24 | cout<> &color) 29 | { 30 | int n = color.size(); 31 | int ans = 0; 32 | for(int a=0; a // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int productSelectionDP(vector> &, int , int ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int k, n; 20 | cin>>k>>n; 21 | vector> a(k, vector(n)); 22 | for(int i=0;i>a[i][j]; 23 | int ans = productSelectionDP(a,k,n); 24 | cout<> &a, int k, int n) 29 | { 30 | vector> dp_table(1<(n, -1)); 31 | for(int x =0;x // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int elevatorRidesOptimal(vector> &, int ,int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int x, n; cin>>x>>n; 20 | vector> a(n); 21 | for(int i=0;i>a[i].first>>a[i].second; 22 | int ans = elevatorRidesOptimal(a, n, x); 23 | cout<> &a, int n, int x) 29 | { 30 | vector> best(1< // all header files 2 | 3 | using namespace std; //namespace created as std 4 | 5 | void recursiveFunction(int n) 6 | { 7 | if(n==0) 8 | return; 9 | 10 | recursiveFunction(n-1); 11 | recursiveFunction(n-1); 12 | } 13 | 14 | int main(void) 15 | { 16 | #ifndef ONLINE_JUDGE 17 | freopen("intput.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | 21 | cout<<"Let's se how many iterations are possible in each time complesity"< 10^5 to 10^7 iterations in one second 23 | * 2. O(n*n) -> 5000 iterations in one second 24 | * 3. O(log(n)) -> n is large around same as like O(1) 25 | * 4. O(2^n) -> 20 iterations in one second because (2^20 == 10^4.5) 26 | * 5. O(n!) -> 10 iterations in one second; 27 | */ 28 | // O(n) 29 | int n = 1e10; // O(N) 30 | clock_t start = clock(); 31 | for(int i=0;i // all header files 3 | 4 | using namespace std; // namespace created as std 5 | 6 | int main(void) 7 | { 8 | #ifndef ONLINE_JUDGE 9 | freopen("input.txt", "r", stdin); 10 | freopen("output.txt", "w", stdout); 11 | clock_t start = clock(); 12 | #endif 13 | 14 | //Code Goes Here 15 | long long int iterations = 1e18; 16 | while(iterations--); 17 | 18 | #ifndef ONLINE_JUDGE 19 | clock_t end = clock(); 20 | cout<<"Total Time Used: "<<(long double)(end - start)/CLOCKS_PER_SEC< // all header files 3 | 4 | using namespace std; //namespace created as std 5 | 6 | int naive_approach(int [], int); 7 | int order_of_n2(int [], int); 8 | int order_of_n1_fast(int [], int); 9 | 10 | int main(void) 11 | { 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | int n = 3; 17 | int a[] = {1, 2, 3}; 18 | cout<<"Naive Approach O(n^3) : "< // all header files 3 | using namespace std; 4 | int main(void) 5 | { 6 | int a[] = {1,2 , 3 ,4 ,5 }; 7 | int n = 5; 8 | int answer = 0; 9 | for(int start_index = 0; start_index < n; start_index+=1) // starting range 10 | { 11 | for(int end_index = start_index; end_index < n;end_index+=1) // range index 12 | { 13 | int sum = 0; // each subarray's sum 14 | for(int range_iterator=start_index; range_iterator<=end_index && range_iterator // all header files 2 | 3 | using namespace std; //namespace created as std 4 | 5 | int main(void) 6 | { 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | 12 | int n; 13 | cin>>n; 14 | int *a = new int[n]; 15 | for(int i=0;i>a[i]; 16 | 17 | sort(a, a+n); 18 | for(int i=0;i> vt; when you sort it then it will be sorted by its first element than the priority will be given to rest of elements 22 | * vecotr> vt; 23 | */ 24 | 25 | return 0; //return type is int 26 | } 27 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 3 (Sorting)/3_vector_sort_reverse_compare: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 3 (Sorting)/3_vector_sort_reverse_compare -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 3 (Sorting)/3_vector_sort_reverse_compare.cpp: -------------------------------------------------------------------------------- 1 | #include // all header files 2 | 3 | using namespace std; // namespace created as std 4 | bool compare(char, char); // compare function for sort 5 | 6 | int main(void) 7 | { 8 | #ifndef ONLINE_JUDGE 9 | freopen("input.txt", "r", stdin); 10 | freopen("output.txt", "w", stdout); 11 | #endif 12 | 13 | int n; 14 | cin>>n; 15 | vector vt(n); // size has been defined 16 | for(int i=0;i>vt[i]; 17 | 18 | sort(vt.begin(), vt.end()); 19 | // ascending order sorted vector ( vector is simply a dynamic array) 20 | for(int i=0;i 0) 38 | return false; 39 | else 40 | return true; 41 | } 42 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 3 (Sorting)/4_binary_search: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 3 (Sorting)/4_binary_search -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 3 (Sorting)/4_binary_search.cpp: -------------------------------------------------------------------------------- 1 | #include // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | int main(void) 6 | { 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | 12 | int n; 13 | cin>>n; 14 | int* a = new int[n]; 15 | for(int i=0;i>a[i]; 16 | int k; 17 | cin>>k; 18 | int left = 0, right=n-1, mid=0; 19 | while(left<=right) 20 | { 21 | mid = left + (right-left)/2; 22 | if(a[mid] == k) 23 | { 24 | break; 25 | } 26 | else if(a[mid] > k) 27 | { 28 | right = mid-1; 29 | } 30 | else 31 | { 32 | left = mid + 1; 33 | } 34 | } 35 | if(a[mid] != k) 36 | cout<<-1< // all header files 2 | 3 | using namespace std; //naemspace created as std 4 | 5 | int main(void) 6 | { 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | int n; 12 | cin>>n; 13 | int* a = new int[n]; 14 | for(int i=0;i>a[i]; 15 | int k; 16 | cin>>k; // number which to be finded 17 | 18 | int index = 0; 19 | for(int b = n/2; b>=1; b/=2) 20 | { 21 | while( (index + b) < n && a[index+b] <= k) 22 | index += b; 23 | } 24 | if(a[index] == k) 25 | cout< // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | int main(void) 6 | { 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | 12 | int n; 13 | cin>>n; 14 | 15 | int* a = new int[n]; 16 | for(int i=0;i>a[i]; 17 | int k; 18 | cin>>k; 19 | // It is compulsory that your array is sorted otherwise this function will fail 20 | auto lb = lower_bound(a, a+n, k) - a; 21 | auto ub = upper_bound(a, a+n, k) - a; 22 | 23 | cout< // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | int main(void) 6 | { 7 | #ifndef ONLINE_JUDGE 8 | freopen("input.txt", "r", stdin); 9 | freopen("output.txt", "w", stdout); 10 | #endif 11 | 12 | int n; 13 | cin>>n; 14 | int* a= new int[n]; 15 | for(int i=0;i>a[i]; 16 | int k; 17 | cin>>k; 18 | // for lower bound 19 | int index =-1; 20 | for(int b = n/2; b>=1; b/=2) 21 | { 22 | while( (index + b) < n && a[index+b] < k) 23 | index += b; 24 | } 25 | // index is the value till where we have elements less than k so our answer is index +1 26 | cout<>a[i]; 32 | index = -1; 33 | for(int b = n/2; b>=1;b/=2) 34 | { 35 | while((index + b) < n && a[index+b] < a[index+b+1]) 36 | index +=b; 37 | } 38 | // same thing is here we are reaching to the element which is 39 | cout< 6 | using namespace std; 7 | 8 | // Important header files 9 | #include // Common file 10 | #include 11 | #include // for less 12 | #include 13 | 14 | 15 | using namespace __gnu_pbds; 16 | using namespace std; 17 | 18 | typedef tree,rb_tree_tag, 19 | tree_order_statistics_node_update> indexed_set; 20 | 21 | int main(void) 22 | { 23 | std::ios::sync_with_stdio(false); cin.tie(0); 24 | 25 | #ifndef ONLINE_JUDGE 26 | freopen("input.txt" , "r", stdin); 27 | freopen("output.txt", "w", stdout); 28 | #endif 29 | 30 | /* 31 | * These data structures are not part of standard c++ but g++ compiler supports them. 32 | * those data structures are called as policy-based data structurs 33 | */ 34 | 35 | indexed_set s; 36 | s.insert(2); 37 | s.insert(3); 38 | s.insert(7); 39 | s.insert(9); 40 | auto x = s.find_by_order(2); 41 | cout << *x << "\n"; // 7 42 | cout << s.order_of_key(7) << "\n"; // 2 43 | 44 | cout << s.order_of_key(6) << "\n"; // 2 45 | cout << s.order_of_key(8) << "\n"; // 3 46 | 47 | return 0; //return type is int 48 | } 49 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 4 (Data Structures)/1_vector_stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 4 (Data Structures)/1_vector_stl -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 4 (Data Structures)/1_vector_stl.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | using namespace std; //namespace created as std 6 | 7 | int main(void) 8 | { 9 | std::ios::sync_with_stdio(false); cin.tie(0); // it makes our program fast 10 | #ifndef ONLINE_JUDGE 11 | freopen("input.txt", "r", stdin); 12 | freopen("output.txt", "w", stdout); 13 | #endif 14 | 15 | // Vector is a Dynamic Array 16 | // vector v; // empty dynamic array 17 | // vector v(size) array will be initialized with 0 as a default value 18 | //vector v(size(), value); we can specify the default value by passing it in a argument 19 | // vector vt = {1, 2, 3, 4 6} // this is also another way of initializing array 20 | int n; 21 | cin>>n; 22 | vector v(n); 23 | for(int i=0;i>v[i]; 24 | // We can also use it as stack push_back 25 | v.push_back(101); 26 | v.pop_back(); 27 | // We will print vector by using for each loop 28 | for(int x: v) 29 | { 30 | cout< str 35 | 36 | string s; 37 | //cin>>s; 38 | getline(cin, s, '\n'); // this line will get the first \n 39 | getline(cin, s, '\n'); 40 | //scanf("%s\n", &s); 41 | // scanf("%[^\n]s, &s); taking input untill we find a new line character \n 42 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); cin.tie(0); 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w",stdout); 15 | #endif 16 | 17 | set st; 18 | int n; 19 | cin>>n; 20 | int val; 21 | for(int i=0;i>val; 24 | st.insert(val); 25 | } 26 | for(auto it: st) 27 | { 28 | cout< s = {2, 5, 56, 38}; 48 | // We can get the size of set by s.size() 49 | 50 | multiset mul; 51 | cin>>n; 52 | for(int i=0;i>val; 55 | mul.insert(val); 56 | } 57 | int k; 58 | cin>>k; // the count of value which should be fined 59 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); cin.tie(0); 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | // Map contains key-value pair 18 | map mp; 19 | mp["monkey"] = 4; 20 | mp["banana"] = 5; 21 | mp["harpisichord"] = 9; 22 | 23 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); cin.tie(0); 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | // Bitset is a data structure which has two values either 0 or 1. It is like an array. 18 | bitset<10> bs; 19 | cin>>bs; 20 | // or bitset<10> bs(string("1010101010")); // 21 | // print the bitset 22 | cout< bit_set(5); // we can give integer as an argument too :) 28 | cout< b1(5); 31 | bitset<10> b2(4); 32 | cout<<(b1&b2)< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); cin.tie(0); 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | // deque supports four operations pust_front, pop_front & push_back, pop_back 18 | // front, back for getting the front and back element in this ds 19 | deque dq; 20 | int n; 21 | cin>>n; 22 | int val; 23 | for(int i=0;i>val; 26 | dq.push_back(val); 27 | dq.push_front(val); 28 | } 29 | cout< // all header files 3 | 4 | #define endl '\n' 5 | 6 | using namespace std; //namespace created as std 7 | 8 | int main(void) 9 | { 10 | std::ios::sync_with_stdio(false); cin.tie(0); 11 | 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | // Stack is a data structure which supports last in first out (LIFO) it has functions like push, pop, top 18 | stack st; 19 | int n; 20 | cin>>n; 21 | for(int i=0;i>val; 25 | st.push(val); 26 | } 27 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); cin.tie(0); 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | // queue is first in first out (FIFO) 18 | // It supports functions like push front pop 19 | queue q; 20 | int n; 21 | cin>>n; 22 | for(int i=0;i>val; 26 | q.push(val); 27 | } 28 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); cin.tie(0); 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | // priority queue is based on the sorted kind of stuff based on the particular value 18 | // max heap 19 | priority_queue pq; 20 | int n; 21 | cin>>n; 22 | for(int i=0;i>val; 26 | pq.push(val); 27 | } 28 | cout<, greater> min_pq; 38 | cin>>n; 39 | for(int i=0;i>val; 43 | min_pq.push(val); 44 | } 45 | cout< // all header files 3 | 4 | #define endl '\n' 5 | 6 | using namespace std; // namespace created as std 7 | 8 | void subsetBacktracking(int*, vector>&, int , int , vector ) ; 9 | 10 | int main(void) 11 | { 12 | std::ios::sync_with_stdio(false); cin.tie(0); 13 | 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | 22 | int* a = new int[n]; 23 | for(int i=0;i>a[i]; 24 | 25 | vector> ans; 26 | vector curr(0); 27 | subsetBacktracking(a,ans, 0, n, curr); 28 | 29 | for(vector subset : ans) 30 | { 31 | if(subset.size()==0) 32 | cout<<' '; 33 | else 34 | cout<> &ans, int i, int n, vector subset) 48 | { 49 | if(i==n) 50 | { 51 | ans.push_back(subset); 52 | return; 53 | } 54 | // first exclude the element from the set 55 | // then include that element in the subset 56 | subset.push_back(a[i]); 57 | subsetBacktracking(a, ans, i+1, n, subset); 58 | subset.pop_back(); 59 | subsetBacktracking(a, ans, i+1, n, subset); 60 | } 61 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/2_generating_subset_powerset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 5 (Complete Search)/2_generating_subset_powerset -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/2_generating_subset_powerset.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | #include // all header files 3 | 4 | #define endl '\n' 5 | 6 | using namespace std; //namespace created as std 7 | void subsetPowerset(vector>&, int *, int ); 8 | int main(void) 9 | { 10 | std::ios::sync_with_stdio(false); cin.tie(0); 11 | 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | int n; 18 | cin>>n; 19 | int* a = new int[n]; 20 | for(int i=0;i>a[i]; 21 | vector> ans; 22 | subsetPowerset(ans, a, n); 23 | for(vector subset: ans) 24 | { 25 | for(int i=0;i<(signed)(subset.size()); i++) 26 | { 27 | cout<> &ans, int* a, int n) 38 | { 39 | // pow(2,n) == (1< subset; 43 | for(int j = 0;j // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | void recursivePermutation(vector>&, int *, int , vector, vector); 10 | 11 | vector> generatePermutation(int*, int); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); cin.tie(0); 16 | 17 | #ifndef ONLINE_JUDGE 18 | freopen("input.txt", "r", stdin); 19 | freopen("output.txt", "w", stdout); 20 | #endif 21 | 22 | int n; 23 | cin>>n; 24 | int* a= new int [n]; 25 | for(int i=0;i>a[i]; 26 | 27 | vector> ans = generatePermutation(a, n); 28 | 29 | for(vector permutation: ans) 30 | { 31 | for(int i=0;i<(signed) permutation.size(); ++i) 32 | cout<> &res, int *a, int n, vector chose, vector current) 40 | { 41 | if((signed)current.size()==n) 42 | { 43 | res.push_back(current); 44 | return; 45 | } 46 | for(int i=0;i> generatePermutation(int *a, int n) 60 | { 61 | vector> res; 62 | vector chose(n, false); 63 | vector current; 64 | recursivePermutation(res, a, n, chose, current); 65 | return res; 66 | } 67 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/4_generating_permutations_with_stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 5 (Complete Search)/4_generating_permutations_with_stl -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/4_generating_permutations_with_stl.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | void permutations(vector> &, int *, int ); 10 | 11 | vector> generatePermutationsSTL(int *, int ); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); cin.tie(0); 16 | 17 | #ifndef ONLINE_JUDGE 18 | freopen("input.txt", "r", stdin); 19 | freopen("output.txt" , "w", stdout); 20 | #endif 21 | 22 | int n; 23 | cin>>n; 24 | int* a = new int[n]; 25 | for(int i=0;i>a[i]; 26 | 27 | vector> ans = generatePermutationsSTL(a, n); 28 | 29 | for(vector permu: ans) 30 | { 31 | for(int i=0;i<(signed)permu.size();i++) 32 | { 33 | cout<> &res, int *a, int n) 42 | { 43 | vector currentPermu; 44 | for(int i=0;i> generatePermutationsSTL(int *a, int n) 55 | { 56 | vector> res; 57 | permutations(res, a, n); 58 | 59 | return res; 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/5_backtracking_queen_placement_not_queen_kills: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 5 (Complete Search)/5_backtracking_queen_placement_not_queen_kills -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/5_backtracking_queen_placement_not_queen_kills.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | void backTrackNQ(long long &, int,int, int * &, int * &, int * &); 10 | 11 | long long backtrackingNQueenPlacment(int ); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); cin.tie(0); 16 | 17 | #ifndef ONLINE_JUDGE 18 | freopen("input.txt", "r", stdin); 19 | freopen("output.txt", "w",stdout); 20 | #endif 21 | 22 | int n; 23 | cin>>n; 24 | long long ans = backtrackingNQueenPlacment(n); 25 | 26 | cout< // all header files 8 | 9 | #define endl '\n' 10 | 11 | using namespace std; //namespace created as std 12 | 13 | long long hash_x[2000005], hash_y[2000005]; 14 | 15 | void calculateSubarray(int *, long long int [], int , int ); 16 | 17 | long long int solvesSubsetSum(int*, int , int); // maximum possible sum less or equal to sum 18 | 19 | 20 | int main(void) 21 | { 22 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 23 | #ifndef ONLINE_JUDGE 24 | freopen("input.txt" ,"r", stdin); 25 | freopen("output.txt", "w", stdout); 26 | #endif 27 | 28 | int n; 29 | cin>>n; 30 | int* a = new int [n]; 31 | for(int i=0;i>a[i]; 32 | int sum; 33 | cin>> sum; // desired sum 34 | cout< max) 72 | max = hash_y[p] + hash_x[i]; 73 | } 74 | } 75 | return max; 76 | } 77 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/input.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 3 34 4 12 5 2 3 | 10 4 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/output.txt: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 5 (Complete Search)/queenMovesOnChessBoard.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 5 (Complete Search)/queenMovesOnChessBoard.gif -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/1_coin_change: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/1_coin_change -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/1_coin_change.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | #include // all header files 3 | 4 | #define endl '\n' 5 | 6 | using namespace std; // namespace created as std 7 | 8 | int maximumCoins(int*, int , int); // maximumCoins 9 | // Greedy alogorithms are not always correct but DP algorithms are! 10 | int main(void) 11 | { 12 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 13 | #ifndef ONLINE_JUDGE 14 | freopen("input.txt", "r", stdin); 15 | freopen("output.txt", "w", stdout); 16 | #endif 17 | 18 | int n; 19 | cin>>n; 20 | int* coins = new int [n]; 21 | for(int i=0;i>coins[i]; 22 | int money; 23 | cin>>money; 24 | // sort the coins 25 | sort(coins, coins + n); // sorted 26 | 27 | cout<=0 && money > 0) 36 | { 37 | ans += money / coins[i]; 38 | money -= (money/ coins[i]) * coins[i]; 39 | i--; 40 | } 41 | 42 | return ans; 43 | } 44 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/2_scheduling_start_finish: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/2_scheduling_start_finish -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/2_scheduling_start_finish.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; //namespace created as std 8 | 9 | vector> maximumTasksScheduling(multiset> , int ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 14 | 15 | #ifndef OLINE_JUDGE 16 | freopen("input.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | 20 | int n; 21 | cin>>n; 22 | multiset > ms; // first-> end_time, second->start_time 23 | for(int i=0;i>start>>end; 27 | ms.insert(make_pair(end, start)); 28 | } 29 | 30 | vector> ans = maximumTasksScheduling(ms, n); 31 | 32 | for(pair pr: ans) 33 | { 34 | cout<> maximumTasksScheduling(multiset> ms, int n) 41 | { 42 | vector> res; 43 | int start = 0, end = 0; 44 | for(pair pr: ms) 45 | { 46 | int currStart = pr.second, currEnd = pr.first; 47 | if( currStart >=start && currStart >=end) 48 | { 49 | start = currStart, end = currEnd; 50 | res.emplace_back(start, end); 51 | } 52 | } 53 | return res; 54 | } 55 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/3_tasks_and_deadline: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/3_tasks_and_deadline -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 6 (Greedy Algorithms)/3_tasks_and_deadline.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector> tasksAndDeadline(int *, int *, int ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | int* duration = new int[n], * deadline = new int[n]; 22 | for(int i=0;i>duration[i]>>deadline[i]; 25 | } 26 | 27 | vector > ans = tasksAndDeadline(duration, deadline, n); 28 | for(pair task: ans) 29 | { 30 | cout<> tasksAndDeadline(int *duration, int* deadline, int n) 37 | { 38 | multiset> ms; 39 | for(int i=0;i> order; 45 | for(pair pr: ms) 46 | { 47 | end += pr.first; 48 | points += pr.second - end; 49 | order.push_back({pr.first, pr.second}); 50 | } 51 | cout<<"Total Score: "< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | int minimizingSums(int*, int , int); 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | int n; 18 | cin>>n; 19 | int* arr = new int[n]; 20 | for(int i=0;i>arr[i]; 22 | int c; 23 | cin>>c; // c is power 24 | /* Optimal answer is ans = (sum of all numbers) / (no of numbers); 25 | * We next consider a problem where we are given n numbers a 1 , a 2 , . . . , a n and our 26 | task is to find a value x that minimizes the sum 27 | | a 1 − x | c + | a 2 − x | c + · · · + | a n − x | c . 28 | We focus on the cases c = 1 and c = 2. 29 | */ 30 | /* 31 | This result can be derived by 32 | presenting the sum as follows: 33 | nx 2 − 2 x ( a 1 + a 2 + · · · + a n ) + ( a 21 + a 22 + · · · + a 2 n ) 34 | The last part does not depend on x , so we can ignore it. The remaining parts 35 | form a function nx 2 − 2 xs where s = a 1 + a 2 + · · · + a n . This is a parabola opening 36 | upwards with roots x = 0 and x = 2 s / n , and the minimum value is the average of 37 | the roots x = s / n , i.e., the average of the numbers a 1 , a 2 , . . . , a n . 38 | * */ 39 | int ans = minimizingSums(arr, n,c); 40 | cout< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | long long int dpCountPossibleCoinCombinations(vector, int , int ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | vector coins(n); 22 | for(int i=0;i>coins[i]; 23 | int amount; 24 | cin>>amount; 25 | cout<<"Counts: "< coins, int n, int amount) 30 | { 31 | vector count(amount+1, 0); 32 | count[0] = 1; 33 | 34 | for(int x = 1; x<=amount; ++x) 35 | { 36 | for(auto c: coins) 37 | { 38 | if( (x-c) >= 0) 39 | { 40 | count[x] += count[x-c]; 41 | /* if the answer is too big 42 | * and the question asked for only mod value 43 | * count[x] += count[x-c] % mod; 44 | * count[x] %= mod; 45 | */ 46 | } 47 | } 48 | } 49 | return count[amount]; 50 | } 51 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/3_longest_increasing_subsequence: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/3_longest_increasing_subsequence -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/3_longest_increasing_subsequence.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | typedef long long ll; 10 | 11 | int longestIncreasingSubseqenceDP(int *, int); // DP solution 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 16 | #ifndef ONLINE_JUDGE 17 | freopen("input.txt", "r",stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | 21 | int n; 22 | cin>>n; 23 | int* num = new int[n]; 24 | for(int i=0;i>num[i]; 25 | 26 | cout< // all header files 3 | 4 | #define endl '\n' 5 | 6 | using namespace std; 7 | 8 | int maximumSumPathDP(int**, int); 9 | 10 | int main(void) 11 | { 12 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 13 | #ifndef ONLINE_JUDGE 14 | freopen("input.txt", "r",stdin); 15 | freopen("output.txt", "w", stdout); 16 | #endif 17 | 18 | int n; 19 | cin>>n; 20 | int** grid = new int*[n]; 21 | for(int i=0;i>grid[i][j]; 27 | } 28 | int ans = maximumSumPathDP(grid, n); // there is condition we can either move down or right 29 | // path from upper left corner to lower right corner 30 | cout<=0 && (j-1) >=0) 44 | dp_table[i][j] = max(dp_table[i-1][j], dp_table[i][j-1]) + grid[i][j]; 45 | else if ( (i-1) >=0) 46 | dp_table[i][j] = dp_table[i-1][j] + grid[i][j]; 47 | else if( (j-1) >=0) 48 | dp_table[i][j] = dp_table[i][j-1] + grid[i][j]; 49 | else 50 | dp_table[i][j] = grid[i][j]; 51 | } 52 | } 53 | return dp_table[n-1][n-1]; 54 | } 55 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/5_knapsack_problem: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/5_knapsack_problem -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/5_knapsack_problem.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | int knapsackDP(int*, int); 10 | 11 | int knapsackDPEfficient(int*, int); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 16 | #ifndef ONLINE_JUDGE 17 | freopen("input.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | 21 | int n; 22 | cin>>n; 23 | int* weight = new int [n]; 24 | for(int i=0;i> weight[i]; 25 | int ans = knapsackDP(weight, n); // using 2d array 26 | 27 | ans = knapsackDPEfficient(weight, n); // using 1d array 28 | 29 | cout<= 0) 54 | dp_table[x][k] |= dp_table[x-weight[k-1]][k-1]; 55 | dp_table[x][k] |= dp_table[x][k-1]; 56 | } 57 | } 58 | 59 | for(int i=0; i<=total_sum; ++i) 60 | { 61 | if(dp_table[i][n]) 62 | ans +=1; 63 | } 64 | 65 | return ans; 66 | } 67 | 68 | int knapsackDPEfficient(int* weight, int n) 69 | { 70 | int ans = 0; 71 | int total_sum = accumulate(weight, weight + n, 0); 72 | bool* dp_table = new bool[total_sum+1]; 73 | 74 | for(int i=0;i<=total_sum; ++i) 75 | dp_table[i] = false; 76 | 77 | 78 | dp_table[0] = true; 79 | for(int k = 1; k<=n; k++) 80 | { 81 | for(int x = total_sum; x >= 0; --x) 82 | { 83 | if(dp_table[x]) 84 | dp_table[x+weight[k-1]] = true; 85 | } 86 | } 87 | 88 | for(int i=0; i<=total_sum; ++i) 89 | { 90 | if(dp_table[i]) 91 | ans +=1; 92 | } 93 | 94 | return ans; 95 | } 96 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/6_edit_distance: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/6_edit_distance -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 7 (Dyanmic Programming)/6_edit_distance.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int editDistanceDP(string s1, string s2); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // fastio 14 | 15 | #ifndef ONLINE_JUDGE 16 | freopen("input.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | 20 | string s1, s2; 21 | cin>>s1>>s2; 22 | 23 | int ans = editDistanceDP(s1,s2); 24 | 25 | cout<0 && j>0) 50 | { 51 | 52 | if(dp_table[i][j] == dp_table[i-1][j] + 1 && s1[i-1] != s2[j-1]) 53 | { 54 | i-=1; 55 | } 56 | else if( dp_table[i][j] == dp_table[i][j-1] +1 && s1[i-1] != s2[j-1]) 57 | { 58 | j-=1; 59 | } 60 | else if(dp_table[i][j] == (dp_table[i-1][j-1] + (s1[i-1] == s2[j-1] ? 0: 1)) && s1[i-1] == s2[j-1]) 61 | { 62 | res = s1[i-1] + res; 63 | i-=1; j-=1; 64 | } 65 | else 66 | i-=1, j-=1; 67 | } 68 | cout< // all header files 4 | 5 | #define endl '\n' 6 | #define pi 3.14 7 | typedef long long ll; 8 | typedef long double ld; 9 | using namespace std; // namespace created as std 10 | 11 | void helperFunc(int , int, int , int, ll**, int , int); 12 | 13 | ll countingTilingsDP(int , int , int , int); 14 | 15 | ll countingTilingsMath(int , int , int , int); 16 | 17 | int main(void) 18 | { 19 | std::ios::sync_with_stdio(false); cin.tie(0); // fastio 20 | #ifndef ONLINE_JUDGE 21 | freopen("input.txt", "r", stdin); 22 | freopen("output.txt", "w", stdout); 23 | #endif 24 | int n, m; 25 | cin>>n>>m; 26 | int a , b; 27 | cin>>a>>b; 28 | ll ans = 0; // countingTilingsDP(n, m, a, b); 29 | ans = countingTilingsMath(n, m, a,b); 30 | 31 | cout<= m) 54 | dp_table[x+1][next_mask] += dp_table[x][mask]; 55 | else 56 | { 57 | int my_mask = 1 << y; 58 | if(mask & my_mask) 59 | helperFunc(x, y+1, mask, next_mask,dp_table, n,m); 60 | else 61 | { 62 | helperFunc(x, y+1, mask, next_mask | my_mask, dp_table, n, m); 63 | if(y+1 // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector> subarraySum(int*, int , int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int n; 19 | cin>>n; 20 | int* a = new int[n]; 21 | int sum = 0; 22 | cin>>sum; 23 | 24 | for(int i=0;i>a[i]; // array 25 | 26 | vector> ans = subarraySum(a, n, sum); 27 | cout<> subarraySum(int *a, int n, int sum) 35 | { 36 | vector> res; 37 | int left, right, currSum; 38 | int count = 0; 39 | for(int i =0 ;i // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespae created as std 8 | 9 | int TwoPointers2SUM(vector &, int, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | vector a(n); 22 | for(int i=0;i>a[i]; 23 | int sum; 24 | cin>>sum; 25 | 26 | int ans = TwoPointers2SUM(a, n, sum); 27 | cout< &a, int n, int sum) 33 | { 34 | sort(a.begin(), a.end()); 35 | int i = 0, j = a.size() -1, ans = 0; 36 | while(i sum) 45 | j-=1; 46 | else 47 | i+=1; 48 | } 49 | return ans; 50 | } 51 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/3_Nearest_Smallest_Element: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/3_Nearest_Smallest_Element -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/3_Nearest_Smallest_Element.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int nearestSmallestElement(vector &, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | vector a(n); 22 | for(int i=0;i>a[i]; 23 | 24 | int ans = nearestSmallestElement(a, n); 25 | cout< &a, int n) 30 | { 31 | stack stk; 32 | for(int i=0;i= a[i]) 35 | stk.pop(); 36 | stk.push(a[i]); 37 | } 38 | return stk.top(); 39 | } 40 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/4_sliding_window_minimum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/4_sliding_window_minimum -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/4_sliding_window_minimum.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector slidingWindowMinimumK(vector &, int , int ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | vector a(n); 22 | for(int i=0;i>a[i]; 23 | int window; 24 | cin>>window; 25 | 26 | vector ans = slidingWindowMinimumK(a, n, window); 27 | for(int x: ans) 28 | cout< slidingWindowMinimumK(vector &a, int n, int k) 35 | { 36 | vector ans; 37 | deque dq; 38 | for(int i=0;i a[i]) dq.pop_back(); 41 | dq.push_back(i); 42 | } 43 | ans.push_back(a[dq.front()]); 44 | for(int i=k;i a[i]) dq.pop_back(); 48 | dq.push_back(i); 49 | ans.push_back(a[dq.front()]); 50 | } 51 | return ans; 52 | } 53 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/input.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 2 1 4 5 3 4 1 2 3 | 4 4 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/output.txt: -------------------------------------------------------------------------------- 1 | 1 1 3 1 1 2 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/subarraySum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 8 (Amortized Analysis)/subarraySum -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 9 (Range Queries)/1_range_sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 9 (Range Queries)/1_range_sum -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 9 (Range Queries)/1_range_sum.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namesapce created as std 8 | 9 | int rangeSum(vector &, int , int , int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r" , stdin); 16 | freopen("output.txt","w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | vector a(n); 22 | for(int i=0;i>a[i]; 23 | int left, right; 24 | cin>>left>>right; 25 | int ans = rangeSum(a, n, left, right); 26 | cout< &a, int n, int left, int right) 32 | { 33 | int sum = 0; 34 | for(int i=left; i<=right;i++) 35 | sum += a[i]; 36 | return sum; 37 | } 38 | -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 9 (Range Queries)/2_static_array_sum_queries_prefix_sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-I Basic Technieques/Chapter 9 (Range Queries)/2_static_array_sum_queries_prefix_sum -------------------------------------------------------------------------------- /Part-I Basic Technieques/Chapter 9 (Range Queries)/2_static_array_sum_queries_prefix_sum.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int staticRangeQuery1D(vector & , int , int); 10 | 11 | int staticRangeQuery2D(vector> &, vector> &xy); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); // fastio 16 | #ifndef ONLINE_JUDGE 17 | freopen("input.txt", "r", stdin); 18 | freopen("output.txt","w",stdout); 19 | #endif 20 | int n; 21 | cin>>n; 22 | vector a(n,0); 23 | for(int i=0;i>a[i]; 24 | int left, right; 25 | cin>>left>>right; 26 | int ans = staticRangeQuery1D(a, left, right); 27 | cout<>row>>col; 31 | vector> matrix(row, vector(col)); 32 | for(int i=0;i>matrix[i][j]; 33 | vector> xy(4); 34 | /* 35 | xy[3]=d xy[2]=c 36 | xy[1]=b xy[0]=a 37 | */ 38 | for(int i=0;i<(signed)xy.size();i++) cin>>xy[i].first>>xy[i].second; 39 | 40 | ans = staticRangeQuery2D(matrix, xy); 41 | cout< &a,int left, int right) 46 | { 47 | int n = a.size(); 48 | vector prefix_sum(n, 0); 49 | prefix_sum[0] = a[0]; 50 | for(int i=1;i> &mat, vector> &xy) 56 | { 57 | int row = mat.size(), col = mat[0].size(); 58 | 59 | vector> prefix_sum_table(row, vector(col, 0)); 60 | prefix_sum_table[0][0] = mat[0][0]; 61 | for(int r=1;r // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int sparseTableMinimumQuery(vector &, int, int , int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int n; 20 | cin>>n; 21 | vector a(n); 22 | for(int i=0;i>a[i]; 23 | int left, right; 24 | cin>>left>>right; 25 | int ans = sparseTableMinimumQuery(a, n, left, right); 26 | cout< &a, int n, int left, int right) 31 | { 32 | int row = n, col = (log(n)/log(2)) + 1; 33 | vector> lookup_table(row, vector(col, 0)); 34 | for(int i=0;i // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void fenwickAdd(vector &, int , int ); 10 | 11 | int fenwickSum(vector &, int ); 12 | 13 | int fenwickTreeBIT(vector&, int , int , int); 14 | 15 | int main(void) 16 | { 17 | std::ios::sync_with_stdio(false); // fastio 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | #endif 22 | int n; 23 | cin>>n; 24 | vector a(n); 25 | for(int i=0;i>a[i]; 26 | int left, right; 27 | cin>>left>>right; 28 | 29 | int ans = fenwickTreeBIT(a, n, left, right); 30 | cout< &fenwick_table, int k, int x) 36 | { 37 | while(k<=(signed)fenwick_table.size()) 38 | { 39 | fenwick_table[k] += x; 40 | k += k&-k; 41 | } 42 | } 43 | 44 | int fenwickSum(vector &fenwick_table, int k) 45 | { 46 | int sum = 0; 47 | while(k>=1) 48 | { 49 | sum += fenwick_table[k]; 50 | k -= k&-k; 51 | } 52 | return sum; 53 | } 54 | 55 | int fenwickTreeBIT(vector &a, int n, int left,int right) 56 | { 57 | vector fenwick_table(n+1, 0); 58 | for(int i=0;i // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | int main(void) 10 | { 11 | std::ios::sync_with_stdio(false); // fastio 12 | #ifndef ONLINE_JUDGE 13 | freopen("input.txt", "r", stdin); 14 | freopen("output.txt", "w", stdout); 15 | #endif 16 | 17 | int vertices, edges; 18 | 19 | // Unweighted graph 20 | cin>>vertices>>edges; 21 | vector> graph_unw(0); 22 | for(int i=0;i>start>>end; 26 | graph_unw.push_back({start, end}); 27 | } 28 | for(int i=0;i "<>vertices>>edges; 34 | vector> graph_w(0); 35 | for(int i=0;i>start>>end>>weight; 39 | graph_w.push_back({start, end, weight}); 40 | } 41 | for(int i=0;i(graph_w[i])+1<<" --> "<(graph_w[i])+1<<" ["<(graph_w[i])<<"]"< 2 2 | 2 --> 3 3 | 2 --> 4 4 | 3 --> 4 5 | 4 --> 1 6 | 7 | 1 --> 2 [5] 8 | 2 --> 3 [7] 9 | 2 --> 4 [6] 10 | 3 --> 4 [5] 11 | 4 --> 1 [2] 12 | 13 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/1_depth_first_search_dfs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 12 (Graph traversal)/1_depth_first_search_dfs -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/1_depth_first_search_dfs.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void dfs(vector> &, vector &,vector &, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r",stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int vertices, edges; 20 | cin>>vertices>>edges; 21 | vector> graph(vertices); 22 | while(edges--) 23 | { 24 | int start, end; 25 | cin>>start>>end; 26 | graph[start].push_back(end); 27 | graph[end].push_back(start); 28 | } 29 | vector visited(vertices, false); 30 | vector ans; 31 | 32 | dfs(graph, visited, ans, 0); 33 | for(int i=0;i> &graph,vector &visited,vector &ans, int current) 40 | { 41 | if(visited[current]) return; 42 | 43 | visited[current] = true; 44 | ans.push_back(current); 45 | for(auto u: graph[current]) 46 | dfs(graph, visited, ans, u); 47 | } 48 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/2_breadth_first_search: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 12 (Graph traversal)/2_breadth_first_search -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/2_breadth_first_search.cpp: -------------------------------------------------------------------------------- 1 | 2 | // @hritikkumar 3 | 4 | #include // all header files 5 | 6 | #define endl '\n' 7 | 8 | using namespace std; // namespace created as std 9 | 10 | vector bfs(vector> &, int); 11 | 12 | int main(void) 13 | { 14 | std::ios::sync_with_stdio(false); // fastio 15 | #ifndef ONLINE_JUDGE 16 | freopen("input.txt", "r",stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | 20 | int vertices, edges; 21 | cin>>vertices>>edges; 22 | vector> graph(vertices); 23 | while(edges--) 24 | { 25 | int start, end; 26 | cin>>start>>end; 27 | graph[start].push_back(end); 28 | graph[end].push_back(start); 29 | } 30 | 31 | vector ans = bfs(graph, 0); 32 | for(int i=0;i bfs(vector> &graph, int current) 39 | { 40 | vector ans(0); 41 | vector visited(graph.size(), false); 42 | vector distance(graph.size(),0); 43 | queue q; q.push(current); 44 | while(!q.empty()) 45 | { 46 | if(!visited[q.front()]) 47 | { 48 | ans.push_back(q.front()), visited[q.front()]=true; 49 | for(int i=0;i<(signed)graph[q.front()].size();i++) 50 | { 51 | if(!visited[graph[q.front()][i]]) 52 | { 53 | q.push(graph[q.front()][i]); 54 | distance[graph[q.front()][i]] = distance[q.front()] + 1; 55 | } 56 | } 57 | } 58 | q.pop(); 59 | } 60 | cout<<"Node"<<" "<<"Distance"< // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void dfs(vector> &, vector &, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r",stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int vertices, edges; 20 | cin>>vertices>>edges; 21 | vector> graph(vertices); 22 | while(edges--) 23 | { 24 | int start, end; 25 | cin>>start>>end; 26 | graph[start].push_back(end); 27 | graph[end].push_back(start); 28 | } 29 | vector visited(vertices, false); 30 | 31 | dfs(graph, visited, 0); 32 | for(auto connection: visited) 33 | { 34 | if(!connection) 35 | { 36 | cout<<"NOT CONNECTED"<> &graph,vector &visited, int current) 45 | { 46 | if(visited[current]) return; 47 | 48 | visited[current] = true; 49 | for(auto u: graph[current]) 50 | dfs(graph, visited, u); 51 | } 52 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/4_finding_cycles: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 12 (Graph traversal)/4_finding_cycles -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/4_finding_cycles.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | bool dfs(vector> &, vector &, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r",stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int vertices, edges; 20 | cin>>vertices>>edges; 21 | vector> graph(vertices); 22 | while(edges--) 23 | { 24 | int start, end; 25 | cin>>start>>end; 26 | graph[start].push_back(end); 27 | graph[end].push_back(start); 28 | } 29 | /*vector visited(vertices, false); 30 | 31 | if(dfs(graph, visited, 0)) 32 | cout<<"CYCLE"<=vertices) 36 | cout<<"CYCLE"<> &graph,vector &visited, int current) 43 | { 44 | visited[current] = true; 45 | for(auto u: graph[current]) 46 | { 47 | if(visited[u]) 48 | return true; 49 | dfs(graph, visited, u); 50 | } 51 | return false; 52 | } 53 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/5_bipartiteness_check: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 12 (Graph traversal)/5_bipartiteness_check -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/5_bipartiteness_check.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | bool dfs(vector> &,vector &, int, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r",stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | int vertices, edges; 20 | cin>>vertices>>edges; 21 | vector> graph(vertices); 22 | while(edges--) 23 | { 24 | int start, end; 25 | cin>>start>>end; 26 | graph[start].push_back(end); 27 | } 28 | vector color(vertices, -1); 29 | bool ans = dfs(graph, color, 0, 1); 30 | if(ans) 31 | { 32 | for(int x: color) 33 | cout<> &graph, vector &color, int current, int setColor) 42 | { 43 | if(color[current] != -1 && color[current] != setColor) 44 | return false; 45 | color[current] = setColor; 46 | for(auto u: graph[current]) 47 | { 48 | if(color[u]==-1) 49 | { 50 | if(!dfs(graph, color, u, 1 - setColor)) 51 | return false; 52 | } 53 | if(color[u] == color[current]) 54 | return false; 55 | } 56 | return true; 57 | } 58 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/input.txt: -------------------------------------------------------------------------------- 1 | 5 5 2 | 0 1 3 | 1 2 4 | 2 4 5 | 4 3 6 | 3 1 7 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 12 (Graph traversal)/output.txt: -------------------------------------------------------------------------------- 1 | 1 0 1 1 0 2 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 13 (Shortest paths)/1_bellman_ford_algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 13 (Shortest paths)/1_bellman_ford_algorithm -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 13 (Shortest paths)/1_bellman_ford_algorithm.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector bellmanFordSPGraph(vector> &, int, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | // If, There is any negative weight cycle in graph then it won't work! 19 | 20 | int vertices, edges; 21 | cin>>vertices>>edges; 22 | vector> graph_edges;//undirected & weighted 23 | for(int i=0;i>start>>end>>weight; 27 | graph_edges.push_back({weight, start, end}); 28 | } 29 | 30 | vector ans = bellmanFordSPGraph(graph_edges, vertices, edges); 31 | cout<<"Vertices Distance"< bellmanFordSPGraph(vector> &graph_edges, int vertices, int edges) 40 | { 41 | vector distance(vertices, (long long int) INT_MAX); 42 | distance[0] = 0; 43 | for(int i=0;i // all header files 5 | 6 | #define endl '\n' 7 | 8 | using namespace std; // namespace created as std 9 | 10 | bool negativeCycleBellmanFord(vector> &, int, int); 11 | 12 | int main(void) 13 | { 14 | std::ios::sync_with_stdio(false); // fastio 15 | #ifndef ONLINE_JUDGE 16 | freopen("input.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | // If, There is any negative weight cycle in graph then it won't work! 20 | 21 | int vertices, edges; 22 | cin>>vertices>>edges; 23 | vector> graph_edges;//undirected & weighted 24 | for(int i=0;i>start>>end>>weight; 28 | graph_edges.push_back({weight, start, end}); 29 | } 30 | 31 | bool ans = negativeCycleBellmanFord(graph_edges, vertices, edges); 32 | if(ans) cout<<"Yes, There is a negative cycle!"<> &graph_edges, int vertices, int edges) 38 | { 39 | vector distance(vertices, (long long int) INT_MAX); 40 | distance[0] = 0; 41 | for(int i=0;i distance[start] + weight) 55 | return true; 56 | } 57 | return false; 58 | } 59 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 13 (Shortest paths)/3_shortest_path_faster_algorithm_spfa_efficient_bellman_ford_algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 13 (Shortest paths)/3_shortest_path_faster_algorithm_spfa_efficient_bellman_ford_algorithm -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 13 (Shortest paths)/3_shortest_path_faster_algorithm_spfa_efficient_bellman_ford_algorithm.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector bellmanFordSPGraph(multiset> &, int, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | // If, There is any negative weight cycle in graph then it won't work! 19 | 20 | int vertices, edges; 21 | cin>>vertices>>edges; 22 | multiset> graph_edges;//undirected & weighted 23 | for(int i=0;i>start>>end>>weight; 27 | graph_edges.insert({weight, start, end}); 28 | } 29 | 30 | vector ans = bellmanFordSPGraph(graph_edges, vertices, edges); 31 | cout<<"Vertices Distance"< bellmanFordSPGraph(multiset> &graph_edges, int vertices, int edges) 40 | { 41 | vector distance(vertices, (long long int) INT_MAX); 42 | distance[0] = 0; 43 | for(int i=0;i // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector dijkstraSPGraph(vector>> &, int , int ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | 19 | // If, The weight is negative in that case it won't work. 20 | 21 | int vertices, edges; cin>>vertices>>edges; 22 | vector>> graph(vertices); 23 | for(int i=0;i>start>>end>>weight; 27 | graph[start].push_back({end, weight}); 28 | graph[end].push_back({start, weight}); 29 | } 30 | vector ans = dijkstraSPGraph(graph, vertices, edges); 31 | cout<<"Vertices Distance"< dijkstraSPGraph(vector>> &graph, int vertices, int edges) 38 | { 39 | vector visited(vertices, false); 40 | vector distance(vertices, (long long int) INT_MAX); 41 | distance[0] = 0; 42 | priority_queue> pq; 43 | pq.push({0,0}); // source from 0th node 44 | while(!pq.empty()) 45 | { 46 | int start = pq.top().second; pq.pop(); 47 | if(visited[start]) continue; 48 | visited[start] = true; 49 | for(auto u:graph[start]) 50 | { 51 | int end, end_weight; 52 | tie(end, end_weight) = u; 53 | distance[end] = min(distance[end], distance[start] + end_weight); 54 | pq.push({-distance[end], end}); 55 | } 56 | } 57 | return distance; 58 | } 59 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 13 (Shortest paths)/5_floyd_warshall_algorithm_shortest_path_first_algorithm_graph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 13 (Shortest paths)/5_floyd_warshall_algorithm_shortest_path_first_algorithm_graph -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 13 (Shortest paths)/5_floyd_warshall_algorithm_shortest_path_first_algorithm_graph.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector> floydWarshallSPAlgorithm(vector> &, int ,int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | // O(n^3) == It gives us all the distance pair. 19 | int vertices, edges; 20 | cin>>vertices>>edges; 21 | vector> graph(vertices, vector(vertices)); 22 | for(int i=0;i>start>>end>>weight; 26 | graph[start][end] = weight; 27 | graph[end][start] = weight; 28 | } 29 | vector> ans = floydWarshallSPAlgorithm(graph, vertices, edges); 30 | cout<<" "; 31 | for(int i=0;i> floydWarshallSPAlgorithm(vector> &graph, int vertices, int edges) 44 | { 45 | vector> distance(vertices, vector(vertices)); 46 | for(int i=0;i // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | void dfs(int, int, vector &, vector> &); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int vertices, edges; cin>>vertices>>edges; 19 | vector> graph(vertices, vector()); 20 | for(int i=0;i>start>>end; 24 | graph[start].push_back(end); 25 | } 26 | vector ans(0); 27 | dfs(0,-1, ans, graph); 28 | for(unsigned int i = 0;i &ans, vector> & graph) 37 | { 38 | ans.push_back(start); 39 | for(auto u: graph[start]) 40 | { 41 | if(u!=end) 42 | dfs(u,start, ans, graph); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/2_no_of_nodes_in_subtree_using_DP: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/2_no_of_nodes_in_subtree_using_DP -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/2_no_of_nodes_in_subtree_using_DP.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | void dfs(int, int, vector &, vector> &); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int vertices, edges; cin>>vertices>>edges; 19 | vector> graph(vertices, vector()); 20 | for(int i=0;i>start>>end; 24 | graph[start].push_back(end); 25 | } 26 | vector ans(vertices, 0); 27 | dfs(0,-1, ans, graph); 28 | for(unsigned int i = 0;i &ans, vector> & graph) 37 | { 38 | ans[start] = 1; 39 | for(auto u: graph[start]) 40 | { 41 | if(u==end) continue; 42 | dfs(u,start, ans, graph); 43 | ans[start] += ans[u]; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/3_tree_diameter: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/3_tree_diameter -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/3_tree_diameter.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void dfs(vector> &,vector &, int ); 10 | 11 | int treeDiameter(vector> &, vector &); 12 | int main(void) 13 | { 14 | std::ios::sync_with_stdio(false); // fastio 15 | #ifndef ONLINE_JUDGE 16 | freopen("input.txt", "r", stdin); 17 | freopen("output.txt", "w", stdout); 18 | #endif 19 | int vertices, edges; 20 | cin>>vertices>>edges; 21 | vector> graph(vertices,vector()); 22 | for(int i=0;i>start>>end; 26 | graph[start].push_back(end); 27 | } 28 | vector toLeaf(vertices, 0), maxLength(vertices, 0); 29 | dfs(graph,toLeaf, 0); 30 | int ans = treeDiameter(graph, toLeaf); 31 | cout<> &graph,vector &toLeaf, int current) 36 | { 37 | for(auto u:graph[current]) 38 | { 39 | dfs(graph,toLeaf, u); 40 | toLeaf[current] = max(toLeaf[current], toLeaf[u] + 1); 41 | } 42 | } 43 | 44 | int treeDiameter(vector> &graph, vector &toLeaf) 45 | { 46 | vector height(graph.size(), 0); 47 | for(int i=0;i<(signed)graph.size();i++) 48 | { 49 | if(graph[i].size()==0) 50 | height[i] = 0; 51 | else if(graph[i].size()==1) 52 | height[i] = toLeaf[graph[i][0]] + 1; 53 | else 54 | { 55 | int max1 = toLeaf[graph[i][0]], max2 = INT_MIN; 56 | for(int j=1;j<(signed)graph[i].size();j++) 57 | { 58 | if(toLeaf[graph[i][j]] > max1) 59 | { 60 | max2 = max1; 61 | max1 = toLeaf[graph[i][j]]; 62 | } 63 | else if(toLeaf[graph[i][j]] > max2) 64 | { 65 | max2 = toLeaf[graph[i][j]]; 66 | } 67 | } 68 | height[i] = max1 + max2 + 2; 69 | } 70 | } 71 | return *max_element(height.begin(), height.end()); 72 | } 73 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/4_tree_diameter_using_DP: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/4_tree_diameter_using_DP -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/4_tree_diameter_using_DP.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void dfs(vector> &, vector &,int , int &, int , int & ); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r", stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int vertices, edges; 19 | cin>>vertices>>edges; 20 | vector> graph(vertices, vector(0)); 21 | for(int i=0;i>start>>end; 25 | graph[start].push_back(end); 26 | graph[end].push_back(start); 27 | } 28 | // diameter of tree => dfs from one node(x) to find the farthest node(a) then again do a dfs from node(a) is node(b) so the answer is distance between a and b (b-a) ans 29 | vector visited(vertices, false); 30 | int x = 0,a = 0,b = 0, maxCount = 0; 31 | dfs(graph, visited, x, a,0, maxCount); 32 | visited.assign(visited.size(), false); 33 | dfs(graph, visited, a,b, 0, maxCount); 34 | cout<<"Diameter: "<> &graph, vector &visited, int root,int &farthestRoot, int distance, int &maxDistance) 39 | { 40 | visited[root] = true; 41 | distance +=1; 42 | for(auto u: graph[root]) 43 | { 44 | if(!visited[u]) 45 | { 46 | if(distance >= maxDistance) 47 | maxDistance = distance, farthestRoot = u; 48 | dfs(graph, visited, u, farthestRoot, distance, maxDistance); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/5_all_longest_path: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/5_all_longest_path -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/6_binary_tree_traversal_inorder_preorder_postorder_traversals: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/6_binary_tree_traversal_inorder_preorder_postorder_traversals -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/6_binary_tree_traversal_inorder_preorder_postorder_traversals.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void preorder(vector> &, vector &, int); 10 | void inorder(vector> &, vector &, int); 11 | void postorder(vector> &, vector &, int); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); // fastio 16 | #ifndef ONLINE_JUDGE 17 | freopen("input.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | int vertices, edges; 21 | cin>>vertices>>edges; 22 | vector> graph(vertices, vector(2, -1)); 23 | for(int i=0;i>start>>end>>side; 27 | if(side == 'L' || side=='l') 28 | graph[start][0] = end; 29 | else if(side=='R'|| side == 'r') 30 | graph[start][1] = end; 31 | } 32 | vector preorder_traversal(0), inorder_traversal(0), postorder_traversal(0); 33 | preorder(graph, preorder_traversal, 0); 34 | inorder(graph, inorder_traversal, 0); 35 | postorder(graph, postorder_traversal, 0); 36 | cout<<"PRE-ORDER: "; 37 | for(int x:preorder_traversal) cout<> &graph, vector &ans, int root) 48 | { 49 | ans.push_back(root); // pre 50 | if(graph[root][0] != -1) 51 | preorder(graph, ans, graph[root][0]); // left 52 | if(graph[root][1] != -1) 53 | preorder(graph, ans, graph[root][1]); // right 54 | } 55 | void inorder(vector> &graph, vector &ans, int root) 56 | { 57 | if(graph[root][0] != -1) 58 | inorder(graph, ans, graph[root][0]); // left 59 | ans.push_back(root); // in 60 | if(graph[root][1] != -1) 61 | inorder(graph, ans, graph[root][1]); // right 62 | } 63 | void postorder(vector> &graph, vector &ans, int root) 64 | { 65 | if(graph[root][0] != -1) 66 | postorder(graph, ans, graph[root][0]); // left 67 | if(graph[root][1] != -1) 68 | postorder(graph, ans, graph[root][1]); // right 69 | ans.push_back(root); // post 70 | } 71 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/input.txt: -------------------------------------------------------------------------------- 1 | 6 5 2 | 2 0 3 | 3 0 4 | 0 1 5 | 1 4 6 | 1 5 7 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/output.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 6 2 | 2 2 3 3 3 3 -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 14 (Tree algorithms)/output.xt: -------------------------------------------------------------------------------- 1 | 2 1 0 1 0 0 0 2 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 15 (Spanning trees)/1_kruskal_minimal_spanning_algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 15 (Spanning trees)/1_kruskal_minimal_spanning_algorithm -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 15 (Spanning trees)/2_prims_minimal_spanning_tree_algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 15 (Spanning trees)/2_prims_minimal_spanning_tree_algorithm -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 15 (Spanning trees)/2_prims_minimal_spanning_tree_algorithm.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | vector> primsAlgorithm(vector>> &, int); 10 | 11 | int main(void) 12 | { 13 | std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // fastio 14 | #ifndef ONLINE_JUDGE 15 | freopen("input.txt", "r",stdin); 16 | freopen("output.txt", "w", stdout); 17 | #endif 18 | int vertices, edges; 19 | cin>>vertices>>edges; 20 | vector>> graph(vertices, vector>(0)); 21 | for(int i=0;i>start>>end>>weight; 25 | graph[start].push_back({end, weight}); 26 | graph[end].push_back({start, weight}); 27 | } 28 | vector> minimalSpanningTree = primsAlgorithm(graph, vertices); 29 | for(int i=0;i<(signed) minimalSpanningTree.size();++i) 30 | { 31 | cout<> primsAlgorithm(vector>> &graph, int vertices) 39 | { 40 | vector> minimalSpanningTree(vertices); 41 | 42 | int source = 0; 43 | vector key(vertices, INT_MAX); 44 | vector parent(vertices, -1); 45 | vector visited(vertices, false); 46 | priority_queue, vector>, greater>> pq; 47 | 48 | pq.push({0, source}); 49 | key[source] = 0; 50 | 51 | while(!pq.empty()) 52 | { 53 | int start = pq.top().second; pq.pop(); 54 | visited[start] = true; 55 | for(auto u: graph[start]) 56 | { 57 | int end = u.first, weight = u.second; 58 | if(!visited[end] && key[end] > weight) 59 | { 60 | key[end] = weight; 61 | pq.push({key[end], end}); 62 | minimalSpanningTree[end]={start, weight}; 63 | } 64 | } 65 | } 66 | return minimalSpanningTree; 67 | } 68 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 15 (Spanning trees)/input.txt: -------------------------------------------------------------------------------- 1 | 6 8 2 | 4 5 2 3 | 0 1 3 4 | 2 5 3 5 | 0 4 5 6 | 1 2 5 7 | 1 4 6 8 | 3 5 7 9 | 2 3 9 10 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 15 (Spanning trees)/output.txt: -------------------------------------------------------------------------------- 1 | 0: {1,3}, {4,5}, 2 | 1: 3 | 2: {5,3}, 4 | 3: {5,7}, 5 | 4: {5,2}, 6 | 5: 7 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/1_topological_sorting_directed_acyclic_graph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 16 (Directed graphs)/1_topological_sorting_directed_acyclic_graph -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/1_topological_sorting_directed_acyclic_graph.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void topoSortDFS(vector> &, vector &,int, vector &); 10 | 11 | vector topologicalSort(vector> &); 12 | 13 | int main(void) 14 | { 15 | std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // fastio 16 | #ifndef ONLINE_JUDGE 17 | freopen("input.txt", "r", stdin); 18 | freopen("output.txt", "w", stdout); 19 | #endif 20 | 21 | int vertices, edges; 22 | cin>>vertices>>edges; 23 | vector> graph(vertices, vector(0)); 24 | for(int i=0;i>start>>end; 28 | graph[start].push_back(end); 29 | } 30 | vector topo = topologicalSort(graph); 31 | for(int x: topo) 32 | cout<> &graph, vector &visited,int root, vector &ans) 38 | { 39 | visited[root] = true; 40 | for(int u: graph[root]) 41 | { 42 | if(!visited[u]) 43 | { 44 | topoSortDFS(graph, visited, u, ans); 45 | } 46 | } 47 | ans.push_back(root); 48 | } 49 | 50 | vector topologicalSort(vector> & graph) 51 | { 52 | vector ans(0); 53 | vector visited(graph.size(), false); 54 | for(int i=0;i<(signed)graph.size();i++) 55 | { 56 | if(!visited[i]) 57 | { 58 | topoSortDFS(graph, visited,i, ans); 59 | } 60 | } 61 | reverse(ans.begin(), ans.end()); 62 | return ans; 63 | } 64 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/2_counting_the_number_of_paths: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 16 (Directed graphs)/2_counting_the_number_of_paths -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/2_counting_the_number_of_paths.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void topoSortDFS(vector> &, vector &,int, vector &); 10 | 11 | vector topologicalSort(vector> &); 12 | 13 | int countNumberOfPathsDP(vector> &, vector &); 14 | 15 | int main(void) 16 | { 17 | std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // fastio 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt", "w", stdout); 21 | #endif 22 | int vertices, edges; 23 | cin>>vertices>>edges; 24 | vector> graph(vertices, vector(0)); 25 | for(int i=0;i>start>>end; 29 | graph[start].push_back(end); 30 | } 31 | vector topo = topologicalSort(graph); 32 | int ans = countNumberOfPathsDP(graph, topo); 33 | cout<> &graph, vector &visited,int root, vector &ans) 38 | { 39 | visited[root] = true; 40 | for(int u: graph[root]) 41 | { 42 | if(!visited[u]) 43 | { 44 | topoSortDFS(graph, visited, u, ans); 45 | } 46 | } 47 | ans.push_back(root); 48 | } 49 | 50 | vector topologicalSort(vector> & graph) 51 | { 52 | vector ans(0); 53 | vector visited(graph.size(), false); 54 | for(int i=0;i<(signed)graph.size();i++) 55 | { 56 | if(!visited[i]) 57 | { 58 | topoSortDFS(graph, visited,i, ans); 59 | } 60 | } 61 | reverse(ans.begin(), ans.end()); 62 | return ans; 63 | } 64 | 65 | int countNumberOfPathsDP(vector> &graph, vector &topoSort) 66 | { 67 | vector dp_table(graph.size(), 0); 68 | dp_table[topoSort[0]] = 1; // We will count the number of nodes 69 | for(int i = 0;i<(signed)topoSort.size();i++) 70 | { 71 | for(int u:graph[topoSort[i]]) 72 | { 73 | dp_table[u] += dp_table[topoSort[i]]; 74 | } 75 | } 76 | return dp_table[topoSort.back()]; 77 | } 78 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/3_extending_dijkstra_algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 16 (Directed graphs)/3_extending_dijkstra_algorithm -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/3_extending_dijkstra_algorithm.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void topoSortDFS(vector>> &, vector &,int, vector &); 10 | 11 | vector topologicalSort(vector>> &); 12 | 13 | int extendedDijkstraAlgorithmDP(vector>> &, vector &); 14 | 15 | int main(void) 16 | { 17 | std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // fastio 18 | #ifndef ONLINE_JUDGE 19 | freopen("input.txt", "r", stdin); 20 | freopen("output.txt","w", stdout); 21 | #endif 22 | int vertices, edges; 23 | cin>>vertices>>edges; 24 | vector>> graph(vertices, vector>(0)); 25 | for(int i=0;i>start>>end>>weight; 29 | graph[start].push_back({end, weight}); 30 | } 31 | vector topo = topologicalSort(graph); 32 | int ans = extendedDijkstraAlgorithmDP(graph, topo); 33 | cout<>> &graph, vector &visited,int root, vector &ans) 38 | { 39 | visited[root] = true; 40 | for(auto u: graph[root]) 41 | { 42 | if(!visited[u.first]) 43 | { 44 | topoSortDFS(graph, visited, u.first, ans); 45 | } 46 | } 47 | ans.push_back(root); 48 | } 49 | 50 | vector topologicalSort(vector>> & graph) 51 | { 52 | vector ans(0); 53 | vector visited(graph.size(), false); 54 | for(int i=0;i<(signed)graph.size();i++) 55 | { 56 | if(!visited[i]) 57 | { 58 | topoSortDFS(graph, visited,i, ans); 59 | } 60 | } 61 | reverse(ans.begin(), ans.end()); 62 | return ans; 63 | } 64 | 65 | int extendedDijkstraAlgorithmDP(vector>> &graph, vector &topoSort) 66 | { 67 | vector dp_table(graph.size(),INT_MAX); 68 | dp_table[topoSort[0]] = 0; 69 | for(int i=0;i<(signed)topoSort.size();++i) 70 | { 71 | for(pair u: graph[topoSort[i]]) 72 | { 73 | dp_table[u.first] = min(dp_table[u.first], dp_table[topoSort[i]] + u.second); 74 | } 75 | } 76 | return dp_table[topoSort.back()]; 77 | } 78 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/4_successor_paths: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 16 (Directed graphs)/4_successor_paths -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/5_floyds_algorithm_cycle_detection: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 16 (Directed graphs)/5_floyds_algorithm_cycle_detection -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/input.txt: -------------------------------------------------------------------------------- 1 | 6 6 2 | 0 1 3 | 1 2 4 | 2 3 5 | 3 4 6 | 4 5 7 | 5 3 8 | 9 | 3 5 10 | 0 11 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 16 (Directed graphs)/output.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 6 2 | 2 3 4 5 6 4 3 | 4 | 1 2 4 5 | 1 2 3 5 6 | 2 3 4 6 7 | 3 4 5 4 8 | 4 5 6 5 9 | 5 6 4 6 10 | 6 4 5 4 11 | succ(3,5): 5 12 | Floyd's Cycle: (3) 3 13 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 17 (Strong connectivity)/1_kosaraju_s_algorithm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 17 (Strong connectivity)/1_kosaraju_s_algorithm -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 17 (Strong connectivity)/1_kosaraju_s_algorithm.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | #define endl '\n' 6 | 7 | using namespace std; // namespace created as std 8 | 9 | void dfsUtil(vector> &graph, int node, stack &stk, vector &visited) 10 | { 11 | visited[node] = true; 12 | for(auto u: graph[node]) 13 | { 14 | if(!visited[u]) 15 | { 16 | dfsUtil(graph, u, stk, visited); 17 | } 18 | } 19 | stk.push(node); 20 | } 21 | 22 | void dfsUtilSC(vector> &reversedGraph, int node, vector &stronglyConnected, vector &visited) 23 | { 24 | visited[node] = true; 25 | stronglyConnected.push_back(node); 26 | for(auto u: reversedGraph[node]) 27 | { 28 | if(!visited[u]) 29 | { 30 | dfsUtilSC(reversedGraph, u, stronglyConnected, visited); 31 | } 32 | } 33 | } 34 | 35 | vector> kosarajuAlgorithm(vector> &graph, vector> &reversedGraph) 36 | { 37 | vector> ans; 38 | vector visited(graph.size(), false); 39 | stack stk; 40 | 41 | // fist time dfs 42 | for(int node = 0; node < (signed) graph.size(); node++) 43 | { 44 | if(!visited[node]) 45 | { 46 | dfsUtil(graph, node, stk, visited); 47 | } 48 | } 49 | 50 | // second time dfs 51 | visited.assign(visited.size(), false); 52 | while(!stk.empty()) 53 | { 54 | int currNode = stk.top(); stk.pop(); 55 | if(!visited[currNode]) 56 | { 57 | vector stronglyConnected; 58 | dfsUtilSC(reversedGraph, currNode, stronglyConnected, visited); 59 | ans.push_back(stronglyConnected); 60 | } 61 | } 62 | return ans; 63 | } 64 | 65 | int main(void) 66 | { 67 | std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // fastio 68 | #ifndef ONLINE_JUDGE 69 | freopen("input.txt", "r", stdin); 70 | freopen("output.txt", "w", stdout); 71 | #endif 72 | 73 | int vertices, edges; 74 | cin>>vertices>>edges; 75 | vector> graph(vertices, vector(0)), reversedGraph(vertices, vector(0)); 76 | for(int i=0;i>start>>end; 80 | graph[start].push_back(end); 81 | reversedGraph[end].push_back(start); 82 | } 83 | vector> ans = kosarajuAlgorithm(graph, reversedGraph); 84 | for(int i=0;i<(signed)ans.size(); i++) 85 | { 86 | cout< [0,0]} {1 => [0,1]} {2 => [0,2]} {3 => [0,3]} {4 => [0,4]} {5 => [0,5]} {6 => [0,6]} {7 => [0,7]} 2 | LCA of 7 and 4 is 1. 3 | LCA of 5 and 4 is 1. 4 | LCA of 2 and 1 is 0. 5 | LCA of 6 and 2 is 2. 6 | LCA of 3 and 2 is 2. 7 | DSU: {0 => [0,7]} {1 => [0,7]} {2 => [0,2]} {3 => [0,6]} {4 => [1,7]} {5 => [0,7]} {6 => [1,6]} {7 => [2,7]} 8 | PRINT 9 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 18 (Tree queries)/output.txt~: -------------------------------------------------------------------------------- 1 | GRAPH: 2 | 0 => {[1, 2],[5, 1],[3, 3],[4, 2],[2, 1],} :::: 1 2 3 4 3 | 1 => {[4, 1],[3, 1],} :::: 5 4 | 2 => {[5, 1],} :::: 5 | 3 => {[1, 1],[4, 1],[3, 2],} :::: 6 7 8 6 | 4 => {[1, 1],} :::: 7 | 5 => {[4, 1],} :::: 8 | 6 => {[4, 1],} :::: 9 | 7 => {[3, 1],} :::: 10 | 8 => {[1, 1],} :::: 11 | ans: 2 12 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/1_Eulerian_Path_Circuits_Graph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/1_Eulerian_Path_Circuits_Graph -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/2_Hamiltonain_Path_Circuit_Graph_Theory: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/2_Hamiltonain_Path_Circuit_Graph_Theory -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/3_De_Bruijn_Sequences_Graph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/3_De_Bruijn_Sequences_Graph -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/3_De_Bruijn_Sequences_Graph.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | using namespace std; 6 | 7 | class DeBruijnGraph 8 | { 9 | public: 10 | unordered_set seen; 11 | vector edges; 12 | string A = "01", deBruijnSequence; 13 | int n, k; 14 | public: 15 | DeBruijnGraph() {} 16 | DeBruijnGraph(int N, int K) 17 | { 18 | this->n = N; 19 | this->k = K; 20 | } 21 | 22 | void runScript() 23 | { 24 | print_everthing(); 25 | deBruijnSequence = deBrujin(); 26 | cout<<"SEQUENCE: "<>N>>K; 77 | DeBruijnGraph* deBruijnGraph = new DeBruijnGraph(N, K); 78 | deBruijnGraph -> runScript(); 79 | 80 | return 0; // return type is int 81 | } 82 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/3_De_Bruijn_Sequences_Graph.cpp~: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | using namespace std; 6 | 7 | class DeBruijnGraph 8 | { 9 | public: 10 | unordered_set seen; 11 | vector edges; 12 | string A = "01", deBruijnSequence; 13 | int n, k; 14 | public: 15 | DeBruijnGraph() {} 16 | DeBruijnGraph(int N, int K) 17 | { 18 | this->n = N; 19 | this->k = K; 20 | } 21 | string deBrujin() 22 | { 23 | seen.clear(); 24 | edges.clear(); 25 | string startingNode = string(n-1, A[0]); 26 | dfs(startingNode); 27 | 28 | string S; 29 | 30 | int l = pow(k, n); 31 | for(int i=0;i>N>>K; 77 | DeBruijnGraph* deBruijnGraph = new DeBruijnGraph(N, K); 78 | deBruijnGraph -> runScript(); 79 | 80 | return 0; // return type is int 81 | } 82 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/4_Knights_Tours_Backtracking: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/4_Knights_Tours_Backtracking -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/4_Knights_Tours_Backtracking.cpp: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | using namespace std; // namespace created as std 6 | 7 | class KnightsTour 8 | { 9 | public: 10 | vector> chessBoard; 11 | int size; 12 | vector moveX = {2, 1, -1, -2, -2, -1, 1, 2}, 13 | moveY = {1, 2, 2, 1, -1, -2,-2, -1}; 14 | public: 15 | KnightsTour() {} 16 | KnightsTour(int N) 17 | { 18 | this->size = N; 19 | chessBoard.resize(N, vector(N, -1)); // chess board of N x N size 20 | } 21 | 22 | void runScript() 23 | { 24 | cout<<"BEFORE:"<=0 && x < size && y >= 0 && y < size 74 | && chessBoard[x][y] == -1 75 | ); 76 | } 77 | 78 | void print_everything() 79 | { 80 | cout<<"N: "<>boardSize; 100 | KnightsTour* knightsTour = new KnightsTour(boardSize); 101 | knightsTour -> runScript(); 102 | 103 | return 0; // return type is int 104 | } 105 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/4_Knights_Tours_Backtracking.cpp~: -------------------------------------------------------------------------------- 1 | // @hritikkumar 2 | 3 | #include // all header files 4 | 5 | using namespace std; // namespace created as std 6 | 7 | class KnightsTour 8 | { 9 | public: 10 | vector> chessBoard; 11 | int size; 12 | vector moveX = {2, 1, -1, -2, -2, -1, 1, 2}, 13 | moveY = {1, 2, 2, 1, -1, -2,-2, -1}; 14 | public: 15 | KnightsTour() {} 16 | KnightsTour(int N) 17 | { 18 | this->size = N; 19 | chessBoard.resize(N, vector(N, -1)); // chess board of N x N size 20 | } 21 | 22 | void runScript() 23 | { 24 | cout<<"BEFORE:"<=0 && x < size && y >= 0 && y < size 75 | && chessBoard[x][y] == -1 76 | ); 77 | } 78 | 79 | void print_everything() 80 | { 81 | cout<<"N: "<>boardSize; 101 | KnightsTour* knightsTour = new KnightsTour(boardSize); 102 | knightsTour -> runScript(); 103 | 104 | return 0; // return type is int 105 | } 106 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/5_Warnsdorffs_Algorithm_Knights_Tour: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/5_Warnsdorffs_Algorithm_Knights_Tour -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/input.txt: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/input.txt~: -------------------------------------------------------------------------------- 1 | 3 2 2 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/knightsMoveOnChessBoard: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/knightsMoveOnChessBoard -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 19 (Paths and circuits)/output.txt: -------------------------------------------------------------------------------- 1 | BEFORE: 2 | N: 8 3 | -1 -1 -1 -1 -1 -1 -1 -1 4 | -1 -1 -1 -1 -1 -1 -1 -1 5 | -1 -1 -1 -1 -1 -1 -1 -1 6 | -1 -1 -1 -1 -1 -1 -1 -1 7 | -1 -1 -1 -1 -1 -1 -1 -1 8 | -1 -1 -1 -1 -1 -1 -1 -1 9 | -1 -1 -1 -1 -1 -1 -1 -1 10 | -1 -1 -1 -1 -1 -1 -1 -1 11 | CHESS BOARD: 12 | 51 32 7 28 47 24 5 26 13 | 8 29 50 57 6 27 48 23 14 | 33 52 31 46 49 56 25 4 15 | 30 9 60 53 58 39 22 55 16 | 43 34 45 38 61 54 3 18 17 | 10 13 42 59 40 19 64 21 18 | 35 44 15 12 37 62 17 2 19 | 14 11 36 41 16 1 20 63 20 | Knights Tour EXIST: 21 | AFTER: 22 | N: 8 23 | 51 32 7 28 47 24 5 26 24 | 8 29 50 57 6 27 48 23 25 | 33 52 31 46 49 56 25 4 26 | 30 9 60 53 58 39 22 55 27 | 43 34 45 38 61 54 3 18 28 | 10 13 42 59 40 19 64 21 29 | 35 44 15 12 37 62 17 2 30 | 14 11 36 41 16 1 20 63 31 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 20 (Flows and cuts)/1_Ford_Fulkerson_Algorithm_Edmonds_Karp_Algorithm_BFS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-II Graph Algorithms/Chapter 20 (Flows and cuts)/1_Ford_Fulkerson_Algorithm_Edmonds_Karp_Algorithm_BFS -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 20 (Flows and cuts)/4_Maximum_Matching_Bipartile_Graph.cpp: -------------------------------------------------------------------------------- 1 | // A C++ program to find maximal 2 | // Bipartite matching. 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // M is number of applicants 8 | // and N is number of jobs 9 | #define M 6 10 | #define N 6 11 | 12 | // A DFS based recursive function 13 | // that returns true if a matching 14 | // for vertex u is possible 15 | bool bpm(bool bpGraph[M][N], int u, 16 | bool seen[], int matchR[]) 17 | { 18 | // Try every job one by one 19 | for (int v = 0; v < N; v++) 20 | { 21 | // If applicant u is interested in 22 | // job v and v is not visited 23 | if (bpGraph[u][v] && !seen[v]) 24 | { 25 | // Mark v as visited 26 | seen[v] = true; 27 | 28 | // If job 'v' is not assigned to an 29 | // applicant OR previously assigned 30 | // applicant for job v (which is matchR[v]) 31 | // has an alternate job available. 32 | // Since v is marked as visited in 33 | // the above line, matchR[v] in the following 34 | // recursive call will not get job 'v' again 35 | if (matchR[v] < 0 || bpm(bpGraph, matchR[v], 36 | seen, matchR)) 37 | { 38 | matchR[v] = u; 39 | return true; 40 | } 41 | } 42 | } 43 | return false; 44 | } 45 | 46 | // Returns maximum number 47 | // of matching from M to N 48 | int maxBPM(bool bpGraph[M][N]) 49 | { 50 | // An array to keep track of the 51 | // applicants assigned to jobs. 52 | // The value of matchR[i] is the 53 | // applicant number assigned to job i, 54 | // the value -1 indicates nobody is 55 | // assigned. 56 | int matchR[N]; 57 | 58 | // Initially all jobs are available 59 | memset(matchR, -1, sizeof(matchR)); 60 | 61 | // Count of jobs assigned to applicants 62 | int result = 0; 63 | for (int u = 0; u < M; u++) 64 | { 65 | // Mark all jobs as not seen 66 | // for next applicant. 67 | bool seen[N]; 68 | memset(seen, 0, sizeof(seen)); 69 | 70 | // Find if the applicant 'u' can get a job 71 | if (bpm(bpGraph, u, seen, matchR)) 72 | result++; 73 | } 74 | return result; 75 | } 76 | 77 | // Driver Code 78 | int main() 79 | { 80 | // Let us create a bpGraph 81 | // shown in the above example 82 | bool bpGraph[M][N] = {{0, 1, 1, 0, 0, 0}, 83 | {1, 0, 0, 1, 0, 0}, 84 | {0, 0, 1, 0, 0, 0}, 85 | {0, 0, 1, 1, 0, 0}, 86 | {0, 0, 0, 0, 0, 0}, 87 | {0, 0, 0, 0, 0, 1}}; 88 | 89 | cout << "Maximum number of applicants that can get job is " 90 | << maxBPM(bpGraph); 91 | 92 | return 0; 93 | } 94 | -------------------------------------------------------------------------------- /Part-II Graph Algorithms/Chapter 20 (Flows and cuts)/5_Path_Cover_Algorithm.cpp: -------------------------------------------------------------------------------- 1 | // Program to print Vertex Cover of a given undirected graph 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | // This class represents a undirected graph using adjacency list 7 | class Graph 8 | { 9 | int V; // No. of vertices 10 | list *adj; // Pointer to an array containing adjacency lists 11 | public: 12 | Graph(int V); // Constructor 13 | void addEdge(int v, int w); // function to add an edge to graph 14 | void printVertexCover(); // prints vertex cover 15 | }; 16 | 17 | Graph::Graph(int V) 18 | { 19 | this->V = V; 20 | adj = new list[V]; 21 | } 22 | 23 | void Graph::addEdge(int v, int w) 24 | { 25 | adj[v].push_back(w); // Add w to v’s list. 26 | adj[w].push_back(v); // Since the graph is undirected 27 | } 28 | 29 | // The function to print vertex cover 30 | void Graph::printVertexCover() 31 | { 32 | // Initialize all vertices as not visited. 33 | bool visited[V]; 34 | for (int i=0; i::iterator i; 38 | 39 | // Consider all edges one by one 40 | for (int u=0; u 3 | #include 4 | using namespace std; 5 | 6 | // This class represents a undirected graph using adjacency list 7 | class Graph 8 | { 9 | int V; // No. of vertices 10 | list *adj; // Pointer to an array containing adjacency lists 11 | public: 12 | Graph(int V); // Constructor 13 | void addEdge(int v, int w); // function to add an edge to graph 14 | void printVertexCover(); // prints vertex cover 15 | }; 16 | 17 | Graph::Graph(int V) 18 | { 19 | this->V = V; 20 | adj = new list[V]; 21 | } 22 | 23 | void Graph::addEdge(int v, int w) 24 | { 25 | adj[v].push_back(w); // Add w to v’s list. 26 | adj[w].push_back(v); // Since the graph is undirected 27 | } 28 | 29 | // The function to print vertex cover 30 | void Graph::printVertexCover() 31 | { 32 | // Initialize all vertices as not visited. 33 | bool visited[V]; 34 | for (int i=0; i::iterator i; 38 | 39 | // Consider all edges one by one 40 | for (int u=0; u // all header files 2 | 3 | #define endl '\n' 4 | 5 | using namespace std; 6 | 7 | class PrimeFactors 8 | { 9 | private: 10 | int number; 11 | vector isPrime; 12 | vector numPrimeFactors; 13 | public: 14 | PrimeFactors() {} 15 | PrimeFactors(int num) 16 | { 17 | isPrime.resize(1e7, true); 18 | this->number = num; 19 | seiveEratosthenes(); 20 | calculatePrimeFactors(); 21 | print_everthing(); 22 | } 23 | 24 | void seiveEratosthenes() 25 | { 26 | isPrime[0] = isPrime[1] = false; 27 | for(int num=2; num < (signed) isPrime.size(); num++) 28 | { 29 | if(isPrime[num] == false) 30 | continue; 31 | 32 | for(int multiples = 2 * num; multiples < (signed)isPrime.size(); multiples += num) 33 | { 34 | isPrime[multiples] = false; 35 | } 36 | } 37 | } 38 | 39 | void calculatePrimeFactors() 40 | { 41 | for(int divider = 2; divider * divider <= number; divider ++) 42 | { 43 | while(number % divider == 0) 44 | { 45 | numPrimeFactors.push_back(divider); 46 | number /= divider; 47 | } 48 | } 49 | if(number > 1) 50 | numPrimeFactors.push_back(number); 51 | } 52 | 53 | int gcd(int a, int b) 54 | { 55 | if(b == 0) 56 | return a; 57 | 58 | return gcd(b, a % b); // swap the values 59 | } 60 | 61 | void print_everthing() 62 | { 63 | cout<<"NUMBER: "<>n; 87 | PrimeFactors* pf = new PrimeFactors(n); 88 | int second_n; 89 | cin>>second_n; 90 | cout<<"GCD("< "<gcd(n, second_n)< // all header files\ 2 | 3 | #define endl '\n' 4 | 5 | using namespace std; 6 | 7 | int main(void) 8 | { 9 | std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // fastio 10 | #ifndef ONLINE_JUDGE 11 | freopen("input.txt", "r", stdin); 12 | freopen("output.txt", "w", stdout); 13 | #endif 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 21 (Number Theory)/1_Primes_and_Fators_Seive_Eratosthenes_Euclid_Algorithm_GCD.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 21 (Number Theory)/1_Primes_and_Fators_Seive_Eratosthenes_Euclid_Algorithm_GCD.o -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 21 (Number Theory)/2_Modular_Arithmetic.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 21 (Number Theory)/2_Modular_Arithmetic.o -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 21 (Number Theory)/input.txt: -------------------------------------------------------------------------------- 1 | 2 5 5 2 | 3 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 21 (Number Theory)/input.txt~: -------------------------------------------------------------------------------- 1 | 2 10 5 2 | 3 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 21 (Number Theory)/output.txt: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:53c234e5e8472b6ac51c1ae1cab3fe06fad053beb8ebfd8977b010655bfdd3c3 3 | size 2 4 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 21 (Number Theory)/output.txt~: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 21 (Number Theory)/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 21 (Number Theory)/test -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 22 (Combinatorics)/1_Binomial_Coefficients: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 22 (Combinatorics)/1_Binomial_Coefficients -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 22 (Combinatorics)/1_Binomial_Coefficients.cpp: -------------------------------------------------------------------------------- 1 | #include // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class BinomialCoefficient 6 | { 7 | public: 8 | int num; 9 | 10 | public: 11 | BinomialCoefficient() 12 | { 13 | // Do nothing 14 | } 15 | BinomialCoefficient(int num) 16 | { 17 | this->num = num; 18 | } 19 | 20 | 21 | int kthCoefficient(int k) 22 | { 23 | // nCk = n!/ (n-k)! * k! 24 | int C[k+1]; 25 | memset(C, 0, sizeof(C)); // setting values as default of 0 26 | C[0] = 1; 27 | for(int i=1;i<=num;i++) 28 | { 29 | for(int j=min(i,k); j>0; j--) 30 | { 31 | C[j] = C[j] + C[j-1]; 32 | } 33 | } 34 | return C[k]; 35 | } 36 | 37 | void print_everything() 38 | { 39 | // Do all the printing tasks 40 | cout<<"NUM: "<>n; 53 | int k; 54 | cin>>k; 55 | BinomialCoefficient * binomialCoefficient = new BinomialCoefficient(n); 56 | binomialCoefficient->print_everything(); // tasking 57 | cout<<"ANS: "<kthCoefficient(k)< // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class BinomialCoefficient 6 | { 7 | public: 8 | int num; 9 | 10 | public: 11 | BinomialCoefficient() 12 | { 13 | // Do nothing 14 | } 15 | BinomialCoefficient(int num) 16 | { 17 | this->num = num; 18 | } 19 | 20 | int kthCoefficient(int k) 21 | { 22 | // nCk = n!/ (n-k)! * k! 23 | int C[k+1]; 24 | memset(C, 0, sizeof(C)); // setting values as default of 0 25 | C[0] = 1; 26 | for(int i=1;i<=num;i++) 27 | { 28 | for(int j=min(i,k); j>0; j--) 29 | { 30 | C[j] = C[j] + C[j-1]; 31 | } 32 | } 33 | return C[k]; 34 | } 35 | 36 | void print_everything() 37 | { 38 | // Do all the printing tasks 39 | cout<<"NUM: "<>n; 52 | int k; 53 | cin>>k; 54 | BinomialCoefficient * binomialCoefficient = new BinomialCoefficient(n); 55 | binomialCoefficient->print_everything(); // tasking 56 | cout<<"ANS: "<kthCoefficient(k)< // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class CatalanNumber 6 | { 7 | private: 8 | int number; 9 | public: 10 | CatalanNumber() 11 | { 12 | this->number = 0; 13 | } 14 | 15 | CatalanNumber(int num) 16 | { 17 | this->number = num; 18 | } 19 | 20 | unsigned long int nCr(int n, int r) 21 | { 22 | unsigned long int res = 1; 23 | // nCr = nC(n-k) 24 | if(r > n-r) 25 | r = n -r; 26 | for(int i=0;i>N; 53 | CatalanNumber *catalanNumber = new CatalanNumber(N); 54 | cout<<"C(N): "<calculateCatalanNumber()< // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class CatalanNumber 6 | { 7 | private: 8 | int number; 9 | public: 10 | CatalanNumber() 11 | { 12 | this->number = 0; 13 | } 14 | 15 | CatalanNumber(int num) 16 | { 17 | this->number = num; 18 | } 19 | 20 | unsigned long int nCr(int n, int r) 21 | { 22 | unsigned long int res = 1; 23 | // nCr = nC(n-k) 24 | if(r > n-r) 25 | r = n -r; 26 | for(int i=0;i>N; 53 | CatalanNumber *catalanNumber = new CatalanNumber(N); 54 | cout<<"C(N): "<calculateCatalanNumber()< // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class Probability 6 | { 7 | public: 8 | int num; 9 | public: 10 | Probability(){} 11 | Probability(int number) 12 | { 13 | this->num = number; 14 | } 15 | 16 | double getProbability() 17 | { 18 | double Pnum = 1.0/ num; 19 | return Pnum; 20 | } 21 | }; 22 | 23 | int main(void) 24 | { 25 | std::ios::sync_with_stdio(false); cin.tie(); cout.tie(0); // fastio 26 | #ifndef ONLINE_JUDGE 27 | freopen("input.txt", "r", stdin); 28 | freopen("output.txt", "w", stdout); 29 | #endif 30 | int n; 31 | cin>>n; 32 | Probability *p = new Probability(n); 33 | cout<<"P(1:"< "<getProbability()< // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class Probability 6 | { 7 | public: 8 | int num; 9 | public: 10 | Probability(){} 11 | Probability(int number) 12 | { 13 | this->num = number; 14 | } 15 | 16 | double getProbability() 17 | { 18 | double Pnum = 1.0/ number; 19 | return Pnum; 20 | } 21 | }; 22 | 23 | int main(void) 24 | { 25 | std::ios::sync_with_stdio(false); cin.tie(); cout.tie(0); // fastio 26 | #ifndef ONLINE_JUDGE 27 | freopen("input.txt", "r", stdin); 28 | freopen("output.txt", "w", stdout); 29 | #endif 30 | int n; 31 | cin>>n; 32 | Probability *p = new Probability(n); 33 | cout<<"P(1:"< "<getProbability()< 0.125 2 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 24 (Probability)/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 24 (Probability)/test -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 25 (Game Theory)/1_GameState.cpp: -------------------------------------------------------------------------------- 1 | #include // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class GameStates 6 | { 7 | public: 8 | int totalNumberOfSticks; 9 | public: 10 | GameStates() 11 | { 12 | totalNumberOfSticks = 0; 13 | } 14 | GameStates(int numberOfSticks) 15 | { 16 | this->totalNumberOfSticks = numberOfSticks; 17 | } 18 | 19 | void canWeWin(int k) 20 | { 21 | if(totalNumberOfSticks % (k+1) == 0) 22 | { 23 | cout<<"LOSING SITUATION!!!"<>n; 42 | GameStates *gS = new GameStates(n); 43 | int k; 44 | cin>>k; 45 | gS -> canWeWin(k); 46 | 47 | return 0; // return type is int 48 | } 49 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 25 (Game Theory)/1_GameState.cpp~: -------------------------------------------------------------------------------- 1 | #include // all header files 2 | 3 | using namespace std; // namespace created as std 4 | 5 | class GameStates 6 | { 7 | public: 8 | int totalNumberOfSticks; 9 | public: 10 | GameStates() 11 | { 12 | totalNumberOfSticks = 0; 13 | } 14 | GameStates(int numberOfSticks) 15 | { 16 | this->totalNumberOfSticks = numberOfSticks; 17 | } 18 | 19 | void canWeWin(int k) 20 | { 21 | if(totalNumberOfSticks % (k+1) == 0) 22 | { 23 | cout<<"LOSING SITUATION!!!"<>n; 42 | GameStates *gS = new GameStates(n); 43 | int k; 44 | cin>>k; 45 | gS -> canWeWin(k); 46 | 47 | return 0; // return type is int 48 | } 49 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 25 (Game Theory)/1_GameState.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 25 (Game Theory)/1_GameState.o -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 25 (Game Theory)/input.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 3 3 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 25 (Game Theory)/input.txt~: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 25 (Game Theory)/input.txt~ -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 25 (Game Theory)/output.txt: -------------------------------------------------------------------------------- 1 | WINNING SITUATION!!! 2 | -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 25 (Game Theory)/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 25 (Game Theory)/test -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 26 (String Algorithms)/input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 26 (String Algorithms)/input.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 26 (String Algorithms)/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 26 (String Algorithms)/output.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 27 (Square Root Algorithms)/input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 27 (Square Root Algorithms)/input.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 27 (Square Root Algorithms)/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 27 (Square Root Algorithms)/output.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 28 (Segment Trees Revised)/input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 28 (Segment Trees Revised)/input.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 28 (Segment Trees Revised)/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 28 (Segment Trees Revised)/output.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 29 (Geometry)/input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 29 (Geometry)/input.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 29 (Geometry)/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 29 (Geometry)/output.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 30 (Sweep Line Algorithms)/input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 30 (Sweep Line Algorithms)/input.txt -------------------------------------------------------------------------------- /Part-III Advanced Topics/Chapter 30 (Sweep Line Algorithms)/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrit-ikkumar/hritikkumar-cp/e5545bf018ce64c852bab9a96240176b15af4916/Part-III Advanced Topics/Chapter 30 (Sweep Line Algorithms)/output.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hritikkumar-cp 2 | -------------------------------------------------------------------------------- /README.md~: -------------------------------------------------------------------------------- 1 | # hritikkumar-cp 2 | -------------------------------------------------------------------------------- /hritikkumar-cp.geany: -------------------------------------------------------------------------------- 1 | [editor] 2 | line_wrapping=false 3 | line_break_column=72 4 | auto_continue_multiline=true 5 | 6 | [file_prefs] 7 | final_new_line=true 8 | ensure_convert_new_lines=false 9 | strip_trailing_spaces=false 10 | replace_tabs=false 11 | 12 | [indentation] 13 | indent_width=4 14 | indent_type=1 15 | indent_hard_tab_width=8 16 | detect_indent=false 17 | detect_indent_width=false 18 | indent_mode=2 19 | 20 | [project] 21 | name=hritikkumar-cp 22 | base_path=/home/hritikkumar/Documents/hritikkumar/hritikkumar-competitiveprogramming/hritikkumar-cp 23 | description= 24 | file_patterns= 25 | 26 | [long line marker] 27 | long_line_behaviour=1 28 | long_line_column=72 29 | 30 | [files] 31 | current_page=2 32 | FILE_NAME_0=3;None;0;EUTF-8;1;1;0;%2Fhome%2Fhritikkumar%2FDocuments%2Fhritikkumar%2Fhritikkumar-competitiveprogramming%2Fhritikkumar-cp%2FPart-III%20Advanced%20Topics%2FChapter%2022%20%28Combinatorics%29%2Finput.txt;0;4 33 | FILE_NAME_1=0;None;0;EUTF-8;1;1;0;%2Fhome%2Fhritikkumar%2FDocuments%2Fhritikkumar%2Fhritikkumar-competitiveprogramming%2Fhritikkumar-cp%2FPart-III%20Advanced%20Topics%2FChapter%2022%20%28Combinatorics%29%2Foutput.txt;0;4 34 | FILE_NAME_2=560;C++;0;EUTF-8;1;1;0;%2Fhome%2Fhritikkumar%2FDocuments%2Fhritikkumar%2Fhritikkumar-competitiveprogramming%2Fhritikkumar-cp%2FPart-III%20Advanced%20Topics%2FChapter%2022%20%28Combinatorics%29%2F1_Binomial_Coefficients.cpp;0;4 35 | 36 | [VTE] 37 | last_dir=/home/hritikkumar/Documents/hritikkumar/hritikkumar-competitiveprogramming/hritikkumar-cp/Part-III Advanced Topics/Chapter 22 (Combinatorics) 38 | 39 | [prjorg] 40 | source_patterns=*.c;*.C;*.cpp;*.cxx;*.c++;*.cc;*.m; 41 | header_patterns=*.h;*.H;*.hpp;*.hxx;*.h++;*.hh; 42 | ignored_dirs_patterns=.*;CVS; 43 | ignored_file_patterns=*.o;*.obj;*.a;*.lib;*.so;*.dll;*.lo;*.la;*.class;*.jar;*.pyc;*.mo;*.gmo; 44 | generate_tag_prefs=0 45 | external_dirs= 46 | 47 | [build-menu] 48 | --------------------------------------------------------------------------------