├── .vscode └── settings.json ├── 2D_arrays ├── 2D_character_arrays_string.cpp ├── 2D_character_arrays_string.exe ├── build.cpp ├── build.exe ├── piyush_magical_park.cpp ├── piyush_magical_park.exe ├── rotate_image.cpp ├── rotate_image.exe ├── spiral_print.cpp ├── spiral_print.exe ├── staircase_search.cpp ├── wave_print.cpp └── wave_print.exe ├── Amazon_Interview_Questions ├── 3sum.cpp ├── Tree │ ├── Boundary_of_bt_premium.cpp │ ├── Levelorder_recursive.cpp │ ├── Levelorder_recursive.exe │ ├── Recover_binarysearch.cpp │ ├── build_balanced_tree.cpp │ ├── build_balanced_tree.exe │ ├── count_nodes.cpp │ ├── count_nodes.exe │ ├── daimeter.cpp │ ├── daimeter.exe │ ├── diameter_eff.cpp │ ├── diameter_eff.exe │ ├── height_balanced_tree.cpp │ ├── lca.cpp │ ├── levelorder_itre_other_app.cpp │ ├── levelordertraversal_iterative.cpp │ ├── levelordertraversal_iterative.exe │ ├── lowest_commom_anc.cpp │ ├── lowest_commom_anc.exe │ ├── preorder_inorder.cpp │ ├── preorder_inorder.exe │ ├── range_sum_of_bst.cpp │ ├── right_view.cpp │ ├── validate_binary_s_t.cpp │ └── zigzagtrav.cpp ├── mergeksortedlists.cpp ├── mergetwolists.cpp ├── trapping_rain_water.cpp ├── twosum.cpp └── twosum.exe ├── Array ├── 10th_ques.cpp ├── 10th_ques.exe ├── 11th_ques.cpp ├── 11th_ques.exe ├── 12th_ques.cpp ├── 12th_ques.exe ├── 13th_ques.cpp ├── 13th_ques.exe ├── 14th_ques.cpp ├── 14th_ques.exe ├── 15th_ques.cpp ├── 15th_ques.exe ├── 16th_ques.cpp ├── 16th_ques.exe ├── 17th_ques.cpp ├── 17th_ques.exe ├── 18th_ques.cpp ├── 18th_ques.exe ├── 19th_ques.cpp ├── 19th_ques.exe ├── 1st_ques.cpp ├── 1st_ques.exe ├── 20th_ques.cpp ├── 21th_ques.cpp ├── 21th_ques.exe ├── 22th_ques.cpp ├── 22th_ques.exe ├── 23th_ques.cpp ├── 24th_ques.cpp ├── 24th_ques.exe ├── 25th_ques.cpp ├── 25th_ques.exe ├── 26th_ques.cpp ├── 26th_ques.exe ├── 27th_ques.cpp ├── 27th_ques.exe ├── 28th_ques.cpp ├── 29th_ques.cpp ├── 29th_ques.exe ├── 2nd_ques.cpp ├── 2nd_ques.exe ├── 30th_ques.cpp ├── 31st_ques.cpp ├── 31st_ques.exe ├── 32th_ques.cpp ├── 32th_ques.exe ├── 33th_ques.cpp ├── 34th_ques.cpp ├── 34th_ques.exe ├── 35th_ques.cpp ├── 36th_ques.cpp ├── 36th_ques.exe ├── 37th_ques.cpp ├── 37th_ques.exe ├── 38th_ques.cpp ├── 38th_ques.exe ├── 39th_ques.cpp ├── 39th_ques.exe ├── 3rd_ques.cpp ├── 3rd_ques.exe ├── 40th_ques.cpp ├── 40th_ques.exe ├── 41th_ques.cpp ├── 42th_ques.cpp ├── 43th_ques.cpp ├── 43th_ques.exe ├── 44th_ques.cpp ├── 44th_ques.exe ├── 45th_ques.cpp ├── 45th_ques.exe ├── 46th_ques.cpp ├── 46th_ques.exe ├── 47th_ques.cpp ├── 47th_ques.exe ├── 48th_ques.cpp ├── 48th_ques.exe ├── 4th_ques.cpp ├── 4th_ques.exe ├── 50th_ques.cpp ├── 50th_ques.exe ├── 5th_ques.cpp ├── 5th_ques.exe ├── 6th_ques.cpp ├── 6th_ques.exe ├── 7th_ques.cpp ├── 7th_ques.exe ├── 8th_ques.cpp ├── 8th_ques.exe ├── 9th_ques.cpp ├── 9th_ques.exe ├── greatest_ele_right_side.cpp ├── sort_array_by_parity.cpp └── sort_colors.cpp ├── Binary_search_trees ├── binary_identical.cpp ├── binary_identical.exe ├── binary_tree_to_bst.cpp ├── binary_tree_to_bst.exe ├── building.cpp ├── building.exe ├── catalan.cpp ├── catalan.exe ├── check_bst.cpp ├── check_bst.exe ├── check_for_bst.cpp ├── check_for_bst.exe ├── construct_bst_preorder.cpp ├── construct_bst_preorder.exe ├── construct_from_preorder.cpp ├── construct_from_preorder.exe ├── count_pair.cpp ├── delete.cpp ├── delete.exe ├── deletion.cpp ├── deletion.exe ├── find_a_value_in_bst.cpp ├── find_a_value_in_bst.exe ├── inorder_succ.cpp ├── inorder_succ.exe ├── kth_largest.cpp ├── kth_largest.exe ├── kth_smallest.cpp ├── largest_bst.cpp ├── largest_bst.exe ├── lca_bst.cpp ├── lca_bst.exe ├── min_max_val_in_bst.cpp ├── min_max_val_in_bst.exe ├── normal_bst_to_balanced_bst.cpp ├── normal_bst_to_balanced_bst.exe ├── restore_bst.cpp ├── restore_bst.exe ├── search.cpp ├── search.exe ├── sorted_array_to_bst.cpp ├── sorted_array_to_bst.exe ├── succ_pred.cpp └── succ_pred.exe ├── Cp ├── Prime_seive.cpp ├── Prime_seive.exe ├── big_factorial.cpp ├── big_factorial.exe ├── birthday_paradox.cpp ├── birthday_paradox.exe ├── findfunc.cpp ├── findfunc.exe ├── stringtokenizer.cpp ├── stringtokenizer.exe ├── unique_numbers2.cpp └── unique_numbers2.exe ├── Dynamic Programming ├── 01_knapsack.cpp ├── 0_1_Knapsack.cpp ├── 0_1_Knapsack.exe ├── Bottom-up-min-coin-change.cpp ├── Bottom-up-min-coin-change.exe ├── Longest_inc_sum_sub.cpp ├── Longest_inc_sum_sub.exe ├── Longest_increasing_subsequence.cpp ├── Longest_increasing_subsequence.exe ├── Top-down-min-coin-change.cpp ├── Top-down-min-coin-change.exe ├── coins_target_ways.cpp ├── coins_target_ways.exe ├── count_all_palindromic_subsq.cpp ├── count_all_palindromic_subsq.exe ├── dp.txt ├── fibnocci1.cpp ├── ladders.cpp ├── ladders.exe ├── ladders_bottom_up.cpp ├── ladders_bottom_up.exe ├── min_cost_frog_jump.cpp ├── min_cost_frog_jump.exe └── minimum_coin_change.cpp ├── FINAL450.xlsx ├── Graph ├── Adjlist.cpp ├── Adjlist.exe ├── Adjlistrep.cpp ├── Adjlistrep.exe ├── BFS.cpp ├── BFS.exe ├── Bfs_cycle_detection.cpp ├── Bfs_cycle_detection.exe ├── Cycle_in_undirected.cpp ├── Find_connected_comp_actual.cpp ├── Find_connected_comp_actual.exe ├── Flood_fill.cpp ├── Flood_fill.exe ├── Prerequisite_tasks.cpp ├── SSSp.cpp ├── SSSp.exe ├── Snakes_ladders.cpp ├── Snakes_ladders.exe ├── cycle_in_undirected1.cpp ├── detect_cycle_in_undirected_graph.cpp ├── dfs_gfg.cpp ├── find_connected_comp.cpp └── find_connected_comp.exe ├── Graph_learning ├── Bipartite_bfs.cpp ├── Bipartite_bfs.exe ├── Dfs.cpp ├── Dfs.exe ├── Intro.cpp ├── Prims_Algo_brute.cpp ├── Prims_Algo_brute.exe ├── dfs_cyclein_undirected.cpp ├── representation.cpp ├── representation.exe ├── shortest_path_from_srctoevery.cpp ├── shortest_path_from_srctoevery.exe ├── shortest_path_from_srctoeverynode_inDAG.cpp ├── shortest_path_from_srctoeverynode_inDAG.exe ├── tempCodeRunnerFile.cpp ├── topological_dfs.cpp ├── topological_dfs.exe ├── toposort_bfs.cpp ├── toposort_bfs.exe ├── weighted_representation.cpp └── weighted_representation.exe ├── Greedy_approach ├── 3_stacks.cpp ├── 3_stacks.exe ├── Activity_selection_problem.cpp ├── Activity_selection_problem.exe ├── Baised.cpp ├── Baised.exe ├── Balife_loadbalancing.cpp ├── Balife_loadbalancing.exe ├── Defkin_spoj.cpp ├── Defkin_spoj.exe ├── burst_ballons.cpp ├── candy_store.cpp ├── candy_store.exe ├── chopstick.cpp ├── chopstick.exe ├── connecting_wires.cpp ├── container_with_most_water.cpp ├── expedition_spoj.cpp ├── expedition_spoj.exe ├── fractional_knapsack.cpp ├── fractional_knapsack.exe ├── indian_coin_change.cpp ├── indian_coin_change.exe ├── island.cpp ├── island.exe ├── job_sequencing_problem.cpp ├── job_sequencing_problem.exe ├── max_len_of_pair_chain.cpp ├── minimum_platforms.cpp ├── minimum_platforms.exe └── tempCodeRunnerFile.cpp ├── Important └── trees │ ├── Flatten_a_bst.cpp │ ├── big_bang_theory_coloring.cpp │ ├── bst_from_preorder.cpp │ └── inorder_preorder_buildtree.cpp ├── Linked_List ├── Delete_Linkedlist.cpp ├── Delete_Linkedlist.exe ├── Deletion_at_key.cpp ├── Deletion_at_key.exe ├── Deletion_recursive.cpp ├── Detect_a_loop.cpp ├── Detect_a_loop.exe ├── Nth_node_from_end.cpp ├── Nth_node_from_end.exe ├── Searching_iterative.cpp ├── Searching_iterative.exe ├── Searching_recursive.cpp ├── Searching_recursive.exe ├── duplicates_sortedll.cpp ├── duplicates_sortedll.exe ├── even_odd.cpp ├── even_odd.exe ├── get_the_Nth_node.cpp ├── get_the_Nth_node.exe ├── insertion_at_end.cpp ├── insertion_at_end.exe ├── insertion_at_front.cpp ├── insertion_at_front.exe ├── insertion_at_node.cpp ├── insertion_at_node.exe ├── intersection_of_two_sorted_ll.cpp ├── intersection_of_two_sorted_ll.exe ├── length_of_loop.cpp ├── length_of_loop.exe ├── linked_list_palindrome.cpp ├── linked_list_palindrome.exe ├── middle_of_Linkedlist.cpp ├── middle_of_Linkedlist.exe ├── middle_of_ll_method2.cpp ├── middle_of_ll_method2.exe ├── move_last_to_front.cpp ├── move_last_to_front.exe ├── reverse.cpp ├── reverse.exe ├── swap_without.cpp ├── swap_without.exe └── unsorted_duplicates.cpp ├── NEW_Graphs ├── output.txt ├── sanket_singh_bfs1.cpp ├── sanket_singh_bfs1.exe ├── sanket_singh_bfs2.cpp ├── striver_lecture2.cpp ├── striver_lecture3.cpp └── striver_lecture4.cpp ├── README.md ├── Recursion ├── recursion1 │ ├── arrayissorted.cpp │ ├── arrayissorted.exe │ ├── binarysearch.cpp │ ├── binarysearch.exe │ ├── factorial.cpp │ ├── factorial.exe │ ├── fibonacci.cpp │ ├── fibonacci.exe │ ├── inc_dec.cpp │ ├── inc_dec.exe │ ├── multiplywithout_multiply_operator.cpp │ ├── multiplywithout_multiply_operator.exe │ ├── power(x,n).cpp │ ├── poweran.cpp │ └── poweran.exe ├── recursion2 │ ├── alloccurences.cpp │ ├── alloccurences.exe │ ├── bubblesort.cpp │ ├── fastpowerrecursion.cpp │ ├── fastpowerrecursion.exe │ ├── first_occurence.cpp │ ├── first_occurence.exe │ ├── inversion_count.cpp │ ├── inversion_count.exe │ ├── lastoccurence.cpp │ ├── lastoccurence.exe │ ├── mergesort.cpp │ ├── mergesort.exe │ └── quicksort.cpp └── recursion3 │ ├── friends_pairing.cpp │ ├── friends_pairing.exe │ ├── friends_pairing_problem.cpp │ ├── friends_pairing_problem.exe │ ├── knapsack_rec.cpp │ ├── knapsack_rec.exe │ ├── ladder_problem.cpp │ ├── ladder_problem.exe │ ├── numbertospelling.cpp │ ├── numbertospelling.exe │ ├── permutations_string.cpp │ ├── phone_keypad.cpp │ ├── phone_keypad.exe │ ├── right_view.cpp │ ├── stringgeneration.cpp │ ├── stringgeneration.exe │ ├── subsequence_generation.cpp │ ├── subsequence_generation.exe │ ├── titling_ways.cpp │ ├── titling_ways.exe │ ├── tower_of_hanoi.cpp │ └── tower_of_hanoi.exe ├── Tree_rev └── levelorder.cpp ├── advanced_backtracking ├── n_queen.cpp ├── n_queen.exe ├── n_queen_bitset.cpp ├── n_queen_bitset.exe ├── n_queen_using_bitmasking.cpp └── n_queen_using_bitmasking.exe ├── amazon oa questions └── Songs_and_genre.cpp ├── backtracking ├── generate_parentheses.cpp ├── generate_parenthesis.cpp ├── generate_parenthesis.exe ├── letter_comb_phone_keypad.cpp ├── n_queen_10.cpp ├── n_queen_10.exe ├── n_queen_leetcode.cpp ├── n_queen_problem.cpp ├── n_queen_problem.exe ├── permutations.cpp ├── permutations.exe ├── permutations_leetcode.cpp ├── rat_in_a_maze.cpp ├── rat_in_a_maze.exe ├── sudoku_solver.cpp ├── sudoku_solver.exe ├── sudoku_solver_leetcode.cpp ├── unique_permutations.cpp └── word-search.cpp ├── binary_tree ├── LCA_bt.cpp ├── LCA_bt.exe ├── Mirror.cpp ├── Sum_tree.cpp ├── all_leaf_nodes_at_same_level.cpp ├── array_implementation.cpp ├── array_implementation.exe ├── balanced.cpp ├── balanced_tree.cpp ├── binary_to_doubly_linked_list.cpp ├── binary_to_doubly_linked_list.exe ├── bottom_view.cpp ├── bottomview.cpp ├── boundary_traversal.cpp ├── boundary_traversal_rev.cpp ├── boundary_traversal_rev.exe ├── check_if_bt_is_sumtree.cpp ├── deletion.cpp ├── deletion_in_bt.cpp ├── diagonal_trav_rev.cpp ├── diagonal_traversal.cpp ├── diameter.cpp ├── diameter.exe ├── diameter_rev.cpp ├── dist_bw_two_nodes_in_bt.cpp ├── dupliactes_subtree.cpp ├── height_iterative.cpp ├── height_iterative.exe ├── height_recursively.cpp ├── height_recursively.exe ├── imsertion.cpp ├── imsertion.exe ├── insertion.cpp ├── insertion.exe ├── intro.cpp ├── intro.exe ├── inverttree.cpp ├── iterative_inorder.cpp ├── iterative_inorder.exe ├── largest_subtreee_sum.cpp ├── largest_subtreee_sum.exe ├── left_view.cpp ├── left_view.exe ├── left_view_rev.cpp ├── level_order_traversal.cpp ├── level_order_traversal.exe ├── levelorder.cpp ├── max_sum_no_two_adjacent.cpp ├── max_sum_no_two_adjacent.exe ├── maximum_depth.cpp ├── mirrortree.cpp ├── mirrortree.exe ├── preorder_inorder.cpp ├── print_all_k_sum_paths.cpp ├── print_all_k_sum_paths.exe ├── reverse_level.cpp ├── reverse_level_leetcode.cpp ├── reverse_level_order_trav.cpp ├── reverse_level_order_trav.exe ├── reverse_level_order_traversal.cpp ├── reverse_level_order_traversal.exe ├── right_view.cpp ├── rightview.cpp ├── sum_of_node_on_longest_path.cpp ├── sum_of_node_on_longest_path.exe ├── sum_tree_rev.cpp ├── top_view.cpp ├── topview.cpp ├── zig_zag_trav.cpp └── zigzag.cpp ├── bit_manipulation ├── basics_1.cpp ├── basics_1.exe ├── xor_1.cpp ├── xor_1.exe ├── xor_2.cpp ├── xor_2.exe ├── xor_3.cpp ├── xor_3.exe └── xor_4.cpp ├── char_array ├── largest_string.cpp ├── largest_string.exe ├── pallindromic_string.cpp ├── pallindromic_string.exe ├── remove_consecutive_duplicate.cpp └── remove_consecutive_duplicate.exe ├── codechef_questions ├── ques1.cpp ├── ques2.cpp ├── ques2.exe ├── ques3.cpp ├── ques3.exe ├── ques4.cpp └── ques4.exe ├── deque ├── intro.cpp ├── intro.exe └── sliding_window_max.cpp ├── graphs ├── 10_interview_questions.cpp ├── Bellman_ford1.cpp ├── Bellman_ford1.exe ├── Depth_first_search_prac1.cpp ├── Depth_first_search_prac1.exe ├── bellman_ford.cpp ├── bellman_ford.exe ├── bfs.cpp ├── bfs.exe ├── bfs1.cpp ├── bfs1.exe ├── bfs_byme.cpp ├── bfs_byme.exe ├── bfs_cc_stl.cpp ├── bfs_disconnected_graph.cpp ├── bfs_disconnected_graph.exe ├── bfs_prac3.cpp ├── bfs_prac3.exe ├── bfs_prac4.cpp ├── bfs_prac4.exe ├── bfs_pract2.cpp ├── bfs_pract2.exe ├── breadth_first_search.cpp ├── breadth_first_search.exe ├── connected_components.cpp ├── connected_components.exe ├── cycle_detect_directed.cpp ├── cycle_detect_directed.exe ├── cycle_directed.cpp ├── cycle_directed.exe ├── depth_first_search.cpp ├── depth_first_search.exe ├── detct_cycle.cpp ├── detct_cycle.exe ├── detect_cycle_undirect.cpp ├── detect_cycle_undirect.exe ├── dfs.cpp ├── dfs.exe ├── dfs_easy.cpp ├── dfs_easy.exe ├── dfs_prac1.cpp ├── dfs_prac1.exe ├── dfs_prac2.cpp ├── dfs_prac2.exe ├── dfs_prac3.cpp ├── dfs_prac3.exe ├── dfs_simple.c++ ├── dfs_stl.cpp ├── dfs_stl.exe ├── dijkstra_pq_ajlist.cpp ├── dijkstra_pq_ajlist.exe ├── dijsktra_algo.cpp ├── dijsktra_algo.exe ├── dtect_cycle_colors.cpp ├── dtect_cycle_colors.exe ├── intro.cpp ├── intro.exe ├── khan_topo_sort.cpp ├── khan_topo_sort.exe ├── mother_vertex.cpp ├── mother_vertex.exe ├── mothervertex.cpp ├── mothervertex.exe ├── number_of_island.cpp ├── number_of_island.exe ├── prism.cpp ├── prism.exe ├── set_implementation.cpp ├── set_implementation.exe ├── topologicalsort.cpp └── topologicalsort.exe ├── hashing ├── another_arraysubset.cpp ├── another_arraysubset.exe ├── array_another_array_subset.cpp ├── array_another_array_subset.exe ├── chaining_hashing.cpp ├── chaining_hashing.exe ├── continuous_subarray_sum.cpp ├── dictionary_samewords.cpp ├── find_the_diff.cpp ├── four_sum.cpp ├── four_sum.exe ├── frequency_count.cpp ├── frequency_count.exe ├── hashmapcustomclass.cpp ├── hashmapcustomclass.exe ├── hashmapstl.cpp ├── hashmapstl.exe ├── highest_lowest.cpp ├── highest_lowest.exe ├── lengthoflongestsubarray.cpp ├── lengthoflongestsubarray.exe ├── longest_consecutive_subsequence.cpp ├── longest_consecutive_subsequence.exe ├── longest_palindrome.cpp ├── longestsubarraywithsumk.cpp ├── longestsubarraywithsumk.exe ├── minimumoperations_samearray.cpp ├── minimumoperations_samearray.exe ├── pair_product.cpp ├── pair_xor.cpp ├── pair_xor.exe ├── palindrome_queries.cpp ├── phonebook.cpp ├── phonebook.exe ├── second_mostrepeated.cpp ├── second_mostrepeated.exe ├── sort_frequency.cpp ├── subarray_0.cpp ├── subarray_0.exe ├── subarraysumzero.cpp ├── subarraysumzero.exe ├── sum_pair.cpp ├── sum_pair.exe ├── union_intersection.cpp ├── union_intersection.exe ├── unordered_map_practice.cpp ├── unordered_map_practice.exe ├── unorderedmap.cpp ├── unorderedmap.exe ├── valid_anagrams_leetcode.cpp ├── valid_sudoku_board.cpp ├── valid_sudoku_board.exe ├── verticalordertraversal.cpp └── verticalordertraversal.exe ├── heap ├── build_max_heap.cpp ├── build_max_heap.exe ├── connect_n_ropes_with_min.cpp ├── connect_n_ropes_with_min.exe ├── heap_from_scratch.cpp ├── heapsort.cpp ├── heapsort.exe ├── is_bt_is_heap.cpp ├── is_bt_is_heap.exe ├── kth_largest_in_a_stream.cpp ├── kth_largest_in_a_stream.exe ├── merge_k_sorted_arrays.cpp ├── merge_k_sorted_arrays.exe ├── mergek_sorted_lists.cpp ├── mergek_sorted_lists.exe ├── minheap_maxheap.cpp └── minheap_maxheap.exe ├── interviewquestions └── reverselinkedlist.cpp ├── leetcode_contest └── the-number-of-the-smallest-unoccupied-chair.cpp ├── mock_practice.cpp ├── linked_list.cpp ├── linked_list.exe ├── reverse_linkedList.cpp ├── stack.cpp ├── stack.exe └── stack_ll.cpp ├── number_theory ├── bits.cpp ├── bits.exe ├── bitset.cpp └── bitset.exe ├── online_assessment ├── cuttoff.cpp ├── movies_on_flight.cpp ├── substring_of_sizek_with_k_distinct_char.cpp └── substring_of_sizek_with_k_distinct_char.exe ├── queues ├── 2nd_approach_stack_imp.cpp ├── 2nd_approach_stack_imp.exe ├── array_impl.cpp ├── array_impl.exe ├── circular_q.cpp ├── circular_q.exe ├── deque.cpp ├── deque.exe ├── generate_binary_num.cpp ├── generate_binary_num.exe ├── linked_list_impl.cpp ├── linked_list_impl.exe ├── minheap_pqueue.cpp ├── minheap_pqueue.exe ├── priority_queue.cpp ├── priority_queue.exe ├── queue_array_intro.cpp ├── queue_intro.exe ├── queue_linked_intro.cpp ├── queue_linked_intro.exe ├── queue_with_stacks.cpp ├── queue_with_stacks.exe ├── reverse_q.cpp ├── reverse_q.exe ├── reversing_queue.cpp └── reversing_queue.exe ├── searching ├── count_squares.cpp ├── count_squares.exe ├── cow_agressive.cpp ├── cow_agressive.exe ├── double_helix.cpp ├── double_helix.exe ├── first_last_occ.cpp ├── first_last_occ.exe ├── fixed_point.cpp ├── fixed_point.exe ├── majority_element.cpp ├── majority_element.exe ├── majority_element1.cpp ├── majority_element1.exe ├── repeating_and_missing.cpp ├── sorted_rotated_array.cpp └── sorted_rotated_array.exe ├── searching_sorting ├── Product_array_puzz.cpp ├── Product_array_puzz.exe ├── binary_search.cpp ├── binary_search.exe ├── interpolation.cpp ├── linear_search.cpp ├── linear_search.exe ├── peak_element.cpp └── peak_element.exe ├── sorting ├── count_inversions.cpp ├── count_inversions.exe ├── count_sort.cpp ├── count_sort.exe ├── dnf_sort.cpp ├── dnf_sort.exe ├── kth_smalles_element.cpp ├── merge_sort.cpp ├── merge_sort.exe ├── merge_two_sorted_arrays.cpp ├── merge_two_sorted_arrays.exe ├── minumum_swaps_to_sort.cpp ├── minumum_swaps_to_sort.exe ├── pair_diff.cpp ├── pair_diff.exe ├── quick_sort.cpp ├── quick_sort.exe ├── reduce_array.cpp ├── reduce_array.exe ├── vector.cpp ├── wave_sort.cpp └── wave_sort.exe ├── sorting_searching ├── bubblesort.cpp ├── bubblesort.exe ├── selectionsort.cpp ├── selectionsort.exe └── tempCodeRunnerFile.cpp ├── stacks ├── Largest_area_in_Histogram.cpp ├── array_implementation.cpp ├── array_implementation.exe ├── array_stack.cpp ├── array_stack.exe ├── balanced_paranthesis.cpp ├── balanced_paranthesis.exe ├── celebrity_prob.cpp ├── celebrity_prob.exe ├── delete_middle.cpp ├── delete_middle.exe ├── find_nearest_supporter.cpp ├── find_nearest_supporter.exe ├── implement_n_stacks_in_an_array.cpp ├── implement_two_stacks_in_an_array.cpp ├── implement_two_stacks_in_an_array.exe ├── infix_to_postfix.cpp ├── infix_to_postfix.exe ├── infix_to_prefix.cpp ├── infix_to_prefix.exe ├── largest_area_in_histo.cpp ├── largest_area_in_histo.exe ├── linked_list_implementation.cpp ├── linked_list_implementation.exe ├── linkedlist_stack.cpp ├── linkedlist_stack.exe ├── longest_valid_ss.cpp ├── longest_valid_ss.exe ├── next_greater_element.cpp ├── next_greater_element.exe ├── next_smaller_ele.cpp ├── next_smaller_ele.exe ├── nextgreaterelement.cpp ├── nextgreaterelement.exe ├── postfix.cpp ├── postfix.exe ├── prefix.cpp ├── prefix.exe ├── redundant_exp.cpp ├── redundant_exp.exe ├── reverse_a_sent.cpp ├── reverse_a_sent.exe ├── reverse_a_stack.cpp ├── reverse_a_stack.exe ├── simplify_directory.cpp ├── simplify_directory.exe ├── special_stack.cpp ├── special_stack.exe ├── stack_using_queue.cpp ├── stack_using_queue.exe ├── stack_using_queue_pop_costly.cpp ├── stack_using_queue_pop_costly.exe ├── stock_span.cpp ├── stock_span.exe ├── stock_span_well.cpp ├── stock_span_well.exe └── trapping_rainwater.cpp └── strings ├── longest_palindromic_subseq.cpp ├── longest_palindromic_subseq.exe ├── permutations.cpp ├── permutations.exe ├── reverse_words.cpp └── reverse_words.exe /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "ios": "cpp", 5 | "vector": "cpp", 6 | "bitset": "cpp", 7 | "algorithm": "cpp", 8 | "unordered_map": "cpp" 9 | } 10 | } -------------------------------------------------------------------------------- /2D_arrays/2D_character_arrays_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | char a[][10] ={{'a','b','c','\0'},{'d','e','f','\0'}}; 8 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a[1000][1000]={0}; 7 | int val=1; 8 | int n,m; 9 | cin>>m>>n; 10 | 11 | for(int row =0; row<=m-1;row++) 12 | { 13 | for(int col =0;col<=n-1;col++) 14 | { 15 | a[row][col]= val; 16 | val = val+1; 17 | 18 | cout< 2 | using namespace std; 3 | 4 | -------------------------------------------------------------------------------- /2D_arrays/wave_print.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/2D_arrays/wave_print.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/3sum.cpp: -------------------------------------------------------------------------------- 1 | // 3sum 2 | // Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. 3 | 4 | // Notice that the solution set must not contain duplicate triplets. 5 | // Example 1: 6 | 7 | // Input: nums = [-1,0,1,2,-1,-4] 8 | // Output: [[-1,-1,2],[-1,0,1]] 9 | // Example 2: 10 | 11 | // Input: nums = [] 12 | // Output: [] 13 | // Example 3: 14 | 15 | // Input: nums = [0] 16 | // Output: [] 17 | //naive approach is o(n^3) 18 | // using three loops 19 | -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/Levelorder_recursive.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/Levelorder_recursive.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/build_balanced_tree.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/build_balanced_tree.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/count_nodes.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/count_nodes.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/daimeter.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/daimeter.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/diameter_eff.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/diameter_eff.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/height_balanced_tree.cpp: -------------------------------------------------------------------------------- 1 | //diff of right and right subtrrees' height should be less than equal to one at every node 2 | // | h1 - h2|<=1 -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/levelorder_itre_other_app.cpp: -------------------------------------------------------------------------------- 1 | // we are printing the elements acc to the levels to identify which element is in which level 2 | -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/levelordertraversal_iterative.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/levelordertraversal_iterative.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/lowest_commom_anc.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/lowest_commom_anc.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/Tree/preorder_inorder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/Tree/preorder_inorder.exe -------------------------------------------------------------------------------- /Amazon_Interview_Questions/trapping_rain_water.cpp: -------------------------------------------------------------------------------- 1 | // trapping rain water 2 | 3 | // [1,4,2,5,6,7,3,2,1] 4 | // left and right 5 | -------------------------------------------------------------------------------- /Amazon_Interview_Questions/twosum.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Amazon_Interview_Questions/twosum.exe -------------------------------------------------------------------------------- /Array/10th_ques.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Array/10th_ques.exe -------------------------------------------------------------------------------- /Array/11th_ques.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void rotate(int arr[], int n) 4 | { 5 | int x= arr[n-1],i; 6 | for(i=n-1;i>0;i--) 7 | { 8 | arr[i]=arr[i-1]; 9 | 10 | } 11 | arr[0]=x; 12 | } 13 | int main() 14 | { 15 | int n; 16 | cin>>n; 17 | int arr[n]; 18 | for(int i=0;i>arr[i]; 21 | } 22 | rotate(arr,n); 23 | for(int i=0;i 2 | using namespace std; 3 | int getmissing(int arr[], int n) 4 | { 5 | int a=arr[0]; 6 | int b=1; 7 | for(int i=1;i 2 | using namespace std; 3 | int getpair(int arr[], int n, int sum) 4 | { 5 | unordered_mapm; 6 | for(int i=0;i 2 | using namespace std; 3 | // void printrep(int arr[], int n) 4 | // { 5 | // int i; 6 | // for(i=0;i=0) 9 | // { 10 | // arr[abs(arr[i])] = -arr[abs(arr[i])]; 11 | 12 | // }else{ 13 | // cout<=n*2) 32 | { 33 | cout< 2 | using namespace std; 3 | void printfirst(int arr[], int n) 4 | { 5 | int min=-1; 6 | setmyset; 7 | for(int i=n-1;i>=0;i--) 8 | { 9 | if(myset.find(arr[i])!=myset.end()) 10 | min=i; 11 | else 12 | myset.insert(arr[i]); 13 | 14 | 15 | } 16 | if(min!=-1) 17 | { 18 | cout<<"the first repeating element is "< 2 | using namespace std; 3 | 4 | int findnonrep(int arr[], int n) 5 | { 6 | unordered_mapmp; 7 | for(int i=0;i 2 | using namespace std; 3 | void solve(int arr[], int n) 4 | { 5 | sort(arr,arr+n); //O(nlog n) 6 | // cout< 2 | #include 3 | using namespace std; 4 | bool subarraysum(int arr[], int n) 5 | { 6 | unordered_setsumset; 7 | int sum=0; 8 | for(int i=0;i 2 | using namespace std; 3 | int maxsubarray(int arr[], int n) 4 | { 5 | int maxsofar= arr[0]; 6 | int currentmax=arr[0]; 7 | for(int i=1;i 2 | using namespace std; 3 | int findmin(int arr[], int low, int high) 4 | { 5 | int mid= low + (high-low)/2; 6 | if(arr[mid] == arr[high]) 7 | { 8 | high--; 9 | } 10 | else if(arr[mid] > arr[high]) 11 | { 12 | low=mid+1; 13 | } 14 | else{ 15 | high= mid; 16 | } 17 | return arr[high]; 18 | } 19 | int main() 20 | { 21 | int arr[]={2,3,4,5,6,7,8,9}; 22 | int n=sizeof(arr)/sizeof(arr[0]); 23 | cout<<" ans is : "< 2 | using namespace std; 3 | void morethan(int arr[], int n, int k) 4 | { 5 | int x= n/k; 6 | unordered_mapmp; 7 | for(int i=0;i x) 13 | { 14 | cout< 2 | using namespace std; 3 | int minarray(int arr[], int n) 4 | { 5 | // return *min_element(arr,arr+n); 6 | return (n==0)?arr[0]:min(arr[0],minarray(arr+1,n-1));// recursively calling the above function 7 | } 8 | int maxarray(int arr[], int n) 9 | { 10 | return *max_element(arr,arr+n); 11 | } 12 | int main() 13 | { 14 | int n; 15 | cin>>n; 16 | int arr[n]; 17 | for(int i=0;i>arr[i]; 20 | } 21 | 22 | cout<<"The minimum element of an array is "< 2 | using namespace std; 3 | 4 | void printrep(int arr[], int n) 5 | { 6 | int *count = new int [sizeof(int)*(n-2)]; 7 | int i; 8 | cout<<" Repeating elements are : "; 9 | for(int i=0;i 2 | using namespace std; 3 | int main() 4 | { 5 | int t,n,m,arr[1000][1000],i,j; 6 | cin>>t; 7 | while(t--) 8 | { 9 | cin>>n; 10 | cin>>m; 11 | for( i=0;i>arr[i][j]; 16 | 17 | } 18 | 19 | } 20 | for(j=0;j 2 | using namespace std; 3 | void findme(int arr[], int n) 4 | { 5 | unordered_mapmp; 6 | for(int i=0;i n/2) 14 | { 15 | count=1; 16 | cout<<"Majority element is "< 2 | using namespace std; 3 | void swap(int *x, int*y) 4 | { 5 | int temp=*x; 6 | *x=*y; 7 | *y=temp; 8 | } 9 | void sorts(int arr[], int n) 10 | { 11 | for(int i=0;i0 && arr[i-1] > arr[i]) 14 | { 15 | swap(arr[i], arr[i-1]); 16 | } 17 | 18 | if(i arr[i]) 19 | { 20 | swap(arr[i], arr[i+1]); 21 | } 22 | } 23 | } 24 | int main() 25 | { 26 | int arr[]={10,90,49,2,1,5,23}; 27 | int n= sizeof(arr)/sizeof(arr[0]); 28 | sorts(arr,n); 29 | for(int i=0;i 2 | using namespace std; 3 | void reverse(int arr[], int start, int end) 4 | { 5 | if(start >= end) 6 | return; 7 | int temp=arr[start]; 8 | arr[start]=arr[end]; 9 | arr[end]=temp; 10 | 11 | reverse(arr,start+1,end-1); 12 | 13 | 14 | } 15 | void printarry(int arr[], int n) 16 | { 17 | for(int i=0;i>n; 27 | int arr[n]; 28 | for(int i=0;i>arr[i]; 31 | } 32 | reverse(arr,0,n-1); 33 | cout<<"the reversed array is "<<" "; 34 | printarry(arr,n); 35 | 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Array/3rd_ques.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Array/3rd_ques.exe -------------------------------------------------------------------------------- /Array/40th_ques.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxwater(int arr[], int n) 5 | { 6 | int res=0; 7 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int arr[]={4,3,6,2,1,1}; 7 | int n=sizeof(arr)/sizeof(arr[0]); 8 | unordered_mapmp; 9 | for(int i: arr) 10 | { 11 | if(mp.find(i)== mp.end()) 12 | { 13 | mp[i]=true; 14 | }else{ 15 | cout<<"Repeating is : "< 2 | using namespace std; 3 | int stock(int arr[], int n) 4 | { 5 | int min_so_far=arr[0]; 6 | int profit=0; 7 | int maxprofit=0; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | void printclosest(int arr[], int n , int x) 5 | { 6 | int res_l,res_r; 7 | int l=0; 8 | int r=n-1; 9 | int diff=INT_MAX; 10 | while(r > l ) 11 | { 12 | if(abs(arr[r]+arr[l]-x) < diff) 13 | { 14 | res_l = l; 15 | res_r = r; 16 | diff = abs(arr[l]+arr[r]-x); 17 | } 18 | 19 | if(arr[l]+arr[r] > x) 20 | r--; 21 | else 22 | l++; 23 | 24 | } 25 | cout<<"The closest pair would be "< 2 | using namespace std; 3 | int las(int arr[], int n) 4 | { 5 | int inc=1; 6 | int dec=1; 7 | for(int i=1;i arr[i-1]) 10 | { 11 | inc=dec+1; 12 | 13 | }else if(arr[i] < arr[i-1]) 14 | { 15 | dec=inc+1; 16 | } 17 | } 18 | return max(inc,dec); 19 | } 20 | int main() 21 | { 22 | int arr[]={10,22,9,33,49,50,31,60}; 23 | int n=sizeof(arr)/sizeof(arr[0]); 24 | cout< 2 | using namespace std; 3 | int kth_smallest(int arr[], int n, int k) 4 | { 5 | sort(arr,arr+n); 6 | return arr[k-1]; 7 | } 8 | int main() 9 | { 10 | int arr[]={12,3,5,7,19}; 11 | int n=sizeof(arr)/sizeof(arr[0]),k=2; 12 | cout<<"Kth smallest element is "< 3 | using namespace std; 4 | unordered_maphm; 5 | void countfreq(int arr[], int n) 6 | { 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | float getmin(int arr[], int n) 5 | { 6 | return *min_element(arr, arr+n); 7 | } 8 | float getmax(int arr[], int n) 9 | { 10 | return *max_element(arr, arr+n); 11 | } 12 | void randc(int arr[], int n) 13 | { 14 | float min= getmin(arr,n); 15 | float max= getmax(arr,n); 16 | float range= max-min; 17 | float coeff = range/ (max+min); 18 | cout<<"range is "< 2 | using namespace std; 3 | 4 | void solve(int arr[], int left, int right) 5 | { 6 | while(left <=right) 7 | { 8 | if(arr[left]<0 && arr[right] <0) left++; 9 | else if(arr[left] >0 && arr[right] <0 ) {swap(arr[left], arr[right]); left++; right--;} 10 | else if(arr[left] >0 && arr[right] >0) right--; 11 | else left++;right--; 12 | 13 | } 14 | } 15 | void display(int arr[], int right) 16 | { 17 | for(int i=0;i<=right;++i) 18 | { 19 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector replaceElements(vector& arr) { 7 | 8 | int n=arr.size(); 9 | int max_ele_so_far=0; 10 | vectorans; 11 | ans.push_back(-1); 12 | for(int i=n-2;i>=0;i--) 13 | { 14 | if(arr[i+1] > max_ele_so_far) 15 | { 16 | ans.push_back(arr[i+1]); 17 | max_ele_so_far=arr[i+1]; 18 | 19 | }else 20 | { 21 | ans.push_back(max_ele_so_far); 22 | } 23 | 24 | } 25 | reverse(ans.begin(), ans.end()); 26 | return ans; 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /Array/sort_array_by_parity.cpp: -------------------------------------------------------------------------------- 1 | // 905. Sort Array By Parity 2 | #include 3 | using namespace std; 4 | class Solution { 5 | public: 6 | vector sortArrayByParity(vector& nums) { 7 | int i=0; 8 | int j=nums.size()-1; 9 | vectorans; 10 | 11 | while(i 2 | using namespace std; 3 | 4 | -------------------------------------------------------------------------------- /Binary_search_trees/binary_identical.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/binary_identical.exe -------------------------------------------------------------------------------- /Binary_search_trees/binary_tree_to_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/binary_tree_to_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/building.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/building.exe -------------------------------------------------------------------------------- /Binary_search_trees/catalan.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/catalan.exe -------------------------------------------------------------------------------- /Binary_search_trees/check_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/check_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/check_for_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/check_for_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/construct_bst_preorder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/construct_bst_preorder.exe -------------------------------------------------------------------------------- /Binary_search_trees/construct_from_preorder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/construct_from_preorder.exe -------------------------------------------------------------------------------- /Binary_search_trees/count_pair.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | -------------------------------------------------------------------------------- /Binary_search_trees/delete.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/delete.exe -------------------------------------------------------------------------------- /Binary_search_trees/deletion.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/deletion.exe -------------------------------------------------------------------------------- /Binary_search_trees/find_a_value_in_bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Node{ 6 | public: 7 | int data; 8 | Node*left,*right; 9 | 10 | Node(int val) 11 | { 12 | data=val; 13 | left=NULL; 14 | right=NULL; 15 | } 16 | }; 17 | 18 | 19 | Node* search(Node*root, int key ) 20 | { 21 | if(root==NULL|| root->data==key) return root; 22 | 23 | if(root->data > key) 24 | { 25 | return search(root->left, key); 26 | } 27 | return search(root->right, key); 28 | } 29 | 30 | 31 | int main() 32 | { 33 | Node*root= new Node(2); 34 | root->left= new Node(1); 35 | root->right = new Node(3); 36 | int key = 3; 37 | cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node*left,*right; 8 | 9 | 10 | Node(int val) 11 | { 12 | data=val; 13 | left=NULL; 14 | right=NULL; 15 | } 16 | }; 17 | 18 | void util(Node* root, int c, int k) 19 | { 20 | 21 | } -------------------------------------------------------------------------------- /Binary_search_trees/largest_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/largest_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/lca_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/lca_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/min_max_val_in_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/min_max_val_in_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/normal_bst_to_balanced_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/normal_bst_to_balanced_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/restore_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/restore_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/search.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/search.exe -------------------------------------------------------------------------------- /Binary_search_trees/sorted_array_to_bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/sorted_array_to_bst.exe -------------------------------------------------------------------------------- /Binary_search_trees/succ_pred.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Binary_search_trees/succ_pred.exe -------------------------------------------------------------------------------- /Cp/Prime_seive.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Cp/Prime_seive.exe -------------------------------------------------------------------------------- /Cp/big_factorial.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Cp/big_factorial.exe -------------------------------------------------------------------------------- /Cp/birthday_paradox.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | float x =1.0; 6 | int people =0; 7 | float num=365; 8 | float denom = 365; 9 | float p ; 10 | while(x > 1-p) 11 | { 12 | // x is less then the threshold 13 | // p = 14 | // x>40% 15 | x = x*(num)/denom; 16 | num--; 17 | people++; 18 | cout<<"People "< 2 | using namespace std; 3 | int main() 4 | { 5 | int arr[]={1,3,4,4,52,1,25,9}; 6 | int n = 8; 7 | int key = 5200; 8 | auto it = find(arr, arr+n, key); 9 | int index = it - arr; 10 | if(index ==n) 11 | { 12 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | char s[100] ="Today is a rainy day"; 6 | char *ptr = strtok(s," "); 7 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int n; 6 | cin>>n; 7 | int cap; 8 | vectorvals(n); 9 | vectorweig(n); 10 | for(int i=0;i>vals[i]; 13 | } 14 | for(int i=0;i>weig[i]; 17 | } 18 | vector>dp(n+1,vector(cap+1)); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Dynamic Programming/0_1_Knapsack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Dynamic Programming/0_1_Knapsack.exe -------------------------------------------------------------------------------- /Dynamic Programming/Bottom-up-min-coin-change.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int mincoins(int n, int coins[], int T) 4 | { 5 | int dp[100]={0}; 6 | // iterate over all states 7 | for(int i=0;i=0) 13 | { 14 | int subprob = dp[n-coins[i]]; 15 | dp[i] = min(dp[i], subprob+1); 16 | } 17 | } 18 | } 19 | return dp[n]; 20 | 21 | } 22 | int main() 23 | { 24 | int n=15; 25 | int coins[]={1,7,10}; 26 | cout< 2 | using namespace std; 3 | long long lis(vector&arr) 4 | { 5 | vectordp(arr.size(),1); 6 | for(int i=1;iarr{2,4,1,6,3,8,10}; 27 | cout<< lis(arr)< 2 | using namespace std; 3 | int dp[1000][1000]; 4 | string s; 5 | int countsq(int i,int j) 6 | { 7 | if(i>j) return 0; 8 | if(i==j) return dp[i][j]=1; 9 | if(dp[i][j]!=-1) return dp[i][j]; 10 | else if(s[i]==s[j]) 11 | { 12 | return dp[i][j] = countsq(i,j-1)+ countsq(i+1,j+1); 13 | }else 14 | { 15 | return dp[i][j] = countsq(i,j-1)+countsq(i+1,j) + countsq(i+1,j-1); 16 | } 17 | 18 | 19 | 20 | } 21 | int main() 22 | { 23 | cin>>s; 24 | int n = s.size(); 25 | memset(dp,-1,sizeof(dp)); 26 | cout<< countsq(0,n-1)< 2 | using namespace std; 3 | int ladders(int n, int k, int dp[]) 4 | { 5 | // base case 6 | if(n==0) return 1; 7 | 8 | // lookup 9 | if(dp[n]!=0) 10 | { 11 | return dp[n]; 12 | } 13 | 14 | 15 | 16 | // rec case 17 | int ways=0; 18 | for(int i=1;i<=k;i++) 19 | { 20 | if(n-i>=0) 21 | ways+=ladders(n-i,k,dp); 22 | 23 | } 24 | return dp[n] = ways; 25 | } 26 | int main() 27 | { 28 | int n,k; 29 | cin>>n>>k; 30 | int dp[100]={0}; 31 | cout< o(k) --> o(nk) 37 | // sc o(n); -------------------------------------------------------------------------------- /Dynamic Programming/ladders.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Dynamic Programming/ladders.exe -------------------------------------------------------------------------------- /Dynamic Programming/ladders_bottom_up.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int ladders(int n, int k) 4 | { 5 | int dp[100]={0}; 6 | if(n==0) return 0; 7 | dp[0]=1; 8 | for(int i=1;i<=n;i++) 9 | { 10 | dp[i]=0; 11 | for(int j=1;j<=k;j++) 12 | { 13 | if(i-j>=0) 14 | dp[i]+=dp[i-j]; 15 | } 16 | } 17 | // see the bottom up 18 | for(int i=0;i<=n;i++) 19 | { 20 | cout<>n>>k; 29 | 30 | cout<< ladders(n,k); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Dynamic Programming/ladders_bottom_up.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Dynamic Programming/ladders_bottom_up.exe -------------------------------------------------------------------------------- /Dynamic Programming/min_cost_frog_jump.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int solve(vectorarr, int n) 4 | { 5 | vectordp(n+1); 6 | dp[0]=0; 7 | dp[1]=0; 8 | for(int i=2;i<=n;i++) 9 | { 10 | dp[i]= min(dp[i-1]+abs(arr[i]-arr[i-1]),dp[i-2]+abs(arr[i]-arr[i-2])); 11 | } 12 | return dp[n]; 13 | 14 | } 15 | 16 | int main() 17 | { 18 | int n; 19 | cin>>n; 20 | vectorarr(n+1); 21 | for(int i=1;i<=n;i++) 22 | { 23 | cin>>arr[i]; 24 | } 25 | cout< adjacency matrix 3 | // 2d array size v*v --> v is the no of vertices of a graph 4 | // pros --> represenattion is easier removing edge can be done in O(1) 5 | // cons: consumes more space o(V^2) 6 | 7 | // adjacency list --> array of list 8 | // unordered_map>mp; 9 | // vectoradj[] --> array of vector 10 | // list*l; // pointer to an array of lists -------------------------------------------------------------------------------- /Graph_learning/Prims_Algo_brute.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Graph_learning/Prims_Algo_brute.exe -------------------------------------------------------------------------------- /Graph_learning/representation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | unordered_map>mp; 4 | void addEdge(int u,int v) 5 | { 6 | mp[u].push_back(v); 7 | mp[v].push_back(u); 8 | 9 | } 10 | void printgraph() 11 | { 12 | for(auto i:mp) 13 | { 14 | cout< "; 15 | for(auto j:mp[i.first]) 16 | { 17 | cout<>adj; 26 | addEdge( 0, 1); 27 | addEdge(0, 4); 28 | addEdge( 1, 2); 29 | addEdge(1, 3); 30 | addEdge( 1, 4); 31 | addEdge( 2, 3); 32 | addEdge( 3, 4); 33 | printgraph(); 34 | cout<<"printed"; 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Graph_learning/representation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Graph_learning/representation.exe -------------------------------------------------------------------------------- /Graph_learning/shortest_path_from_srctoevery.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Graph_learning/shortest_path_from_srctoevery.exe -------------------------------------------------------------------------------- /Graph_learning/shortest_path_from_srctoeverynode_inDAG.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Graph_learning/shortest_path_from_srctoeverynode_inDAG.exe -------------------------------------------------------------------------------- /Graph_learning/tempCodeRunnerFile.cpp: -------------------------------------------------------------------------------- 1 | push -------------------------------------------------------------------------------- /Graph_learning/topological_dfs.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Graph_learning/topological_dfs.exe -------------------------------------------------------------------------------- /Graph_learning/toposort_bfs.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Graph_learning/toposort_bfs.exe -------------------------------------------------------------------------------- /Graph_learning/weighted_representation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Graph_learning/weighted_representation.exe -------------------------------------------------------------------------------- /Greedy_approach/3_stacks.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/3_stacks.exe -------------------------------------------------------------------------------- /Greedy_approach/Activity_selection_problem.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/Activity_selection_problem.exe -------------------------------------------------------------------------------- /Greedy_approach/Baised.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/Baised.exe -------------------------------------------------------------------------------- /Greedy_approach/Balife_loadbalancing.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/Balife_loadbalancing.exe -------------------------------------------------------------------------------- /Greedy_approach/Defkin_spoj.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/Defkin_spoj.exe -------------------------------------------------------------------------------- /Greedy_approach/burst_ballons.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findMinArrowShots(vector>& points) { 5 | sort(points.begin(),points.end()); 6 | int i=0,count=0,currend; 7 | while(i 2 | using namespace std; 3 | 4 | int findminimum(int arr[], int n, int k) 5 | { 6 | int res=0; 7 | for(int i=0;i=index;i--) 22 | { 23 | res+=arr[i]; 24 | index+=k; 25 | 26 | } 27 | return res; 28 | 29 | } 30 | int main() 31 | { 32 | int arr[]={3,2,1,4}; 33 | int n=sizeof(arr)/sizeof(arr[0]); 34 | int k=2; 35 | sort(arr,arr+n); 36 | cout<< findminimum(arr,n,k)< 2 | using namespace std; 3 | int connect(int white[], int black[], int m , int n) 4 | { 5 | sort(white,white+m); 6 | sort(black,black+m); 7 | 8 | int i=0,j=0; 9 | while(i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maxArea(vector& height) { 7 | int ans = 0, l = 0, r = height.size() - 1; 8 | while(l < r){ 9 | ans = max(ans, min(height[l], height[r])*(r - l)); 10 | height[l] < height[r] ? l++ : r--; 11 | } 12 | return ans; 13 | } 14 | }; -------------------------------------------------------------------------------- /Greedy_approach/expedition_spoj.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/expedition_spoj.exe -------------------------------------------------------------------------------- /Greedy_approach/fractional_knapsack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/fractional_knapsack.exe -------------------------------------------------------------------------------- /Greedy_approach/indian_coin_change.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int make_change(int coins[], int n, int money) 5 | { 6 | int ans=0; 7 | while(money > 0) 8 | { 9 | 10 | int index = upper_bound(coins,coins+n,money)- 1 - coins; 11 | // cout<>money; 23 | int coins[]={1,2,5,10,20,50,100,200,1000}; 24 | int t = sizeof(coins)/sizeof(coins[0]); 25 | 26 | cout<<" No of coins needed "<< make_change(coins,t,money); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Greedy_approach/indian_coin_change.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Greedy_approach/indian_coin_change.exe -------------------------------------------------------------------------------- /Greedy_approach/island.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int s,n,m; 7 | cin>>s>>n>>m; 8 | int x=s/7; 9 | int y= s - x; 10 | int sm = s*m; 11 | int days= sm/n; 12 | if(sm%n!=0) 13 | { 14 | days++; 15 | } 16 | if(days <=y) 17 | cout<<"Yes"< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | static bool comp(vector& a, vector& b) 7 | { 8 | if(a[1]==b[1]) return a[0]>& pairs) { 12 | sort(pairs.begin(),pairs.end(),comp); 13 | int ans=1; 14 | int curr =pairs[0][1]; 15 | for(int i=1;i 2 | using namespace std; 3 | class linkedlist{ 4 | public: 5 | 6 | }; 7 | int main() 8 | { 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Important/trees/big_bang_theory_coloring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node{ 4 | public: 5 | int val; 6 | node*left,*right; 7 | node(int d) 8 | { 9 | val = d; 10 | left = right =NULL; 11 | } 12 | }; 13 | node*createtree() 14 | { 15 | int data; 16 | cin>>data; 17 | if((data)==-1) return NULL; 18 | node*root= new node(data); 19 | root->left =createtree(); 20 | root->right=createtree(); 21 | return root; 22 | } 23 | int findnode() 24 | { 25 | 26 | } 27 | int bigbang(node*root, int x,int n) 28 | { 29 | 30 | } 31 | 32 | int main() 33 | { 34 | int N,n,x; 35 | cin>>N>>n>>x; 36 | node*root= createtree(); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Important/trees/bst_from_preorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class TreeNode{ 4 | public: 5 | int val; 6 | TreeNode*left,*right; 7 | 8 | }; 9 | TreeNode*getnode(int val) 10 | { 11 | TreeNode*temp = new TreeNode; 12 | temp->val = val; 13 | temp->left=temp->right =NULL; 14 | return temp; 15 | 16 | 17 | } 18 | class Solution{ 19 | public: 20 | 21 | TreeNode* bstFromPreorder(vector& preorder) { 22 | int n= preorder.size(); 23 | if(n==0) return NULL; 24 | 25 | } 26 | 27 | }; 28 | 29 | int main() 30 | { 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Linked_List/Delete_Linkedlist.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/Delete_Linkedlist.exe -------------------------------------------------------------------------------- /Linked_List/Deletion_at_key.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/Deletion_at_key.exe -------------------------------------------------------------------------------- /Linked_List/Deletion_recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int info; 6 | node* link =NULL; 7 | node(){} 8 | node(int a):info(a){} 9 | }; 10 | 11 | void deletenode(node*& head, int val) 12 | { 13 | if(head==NULL) 14 | { 15 | cout<<"Element not prsent \n"; 16 | } 17 | 18 | if(head->info ==val) 19 | { 20 | node*t =head; 21 | head =head->link; 22 | 23 | delete(t); 24 | return; 25 | } 26 | deletenode(head->link, val); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Linked_List/Detect_a_loop.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/Detect_a_loop.exe -------------------------------------------------------------------------------- /Linked_List/Nth_node_from_end.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/Nth_node_from_end.exe -------------------------------------------------------------------------------- /Linked_List/Searching_iterative.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/Searching_iterative.exe -------------------------------------------------------------------------------- /Linked_List/Searching_recursive.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/Searching_recursive.exe -------------------------------------------------------------------------------- /Linked_List/duplicates_sortedll.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/duplicates_sortedll.exe -------------------------------------------------------------------------------- /Linked_List/even_odd.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/even_odd.exe -------------------------------------------------------------------------------- /Linked_List/get_the_Nth_node.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/get_the_Nth_node.exe -------------------------------------------------------------------------------- /Linked_List/insertion_at_end.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/insertion_at_end.exe -------------------------------------------------------------------------------- /Linked_List/insertion_at_front.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node*next; 8 | }; 9 | 10 | void push(Node** head, int data) 11 | { 12 | Node* newnode =new Node(); 13 | newnode->data =data; 14 | newnode->next =(*head); 15 | (*head) = newnode; 16 | 17 | } 18 | void printlist(Node*node) 19 | { 20 | while(node!=NULL) 21 | { 22 | cout<data<<"->"; 23 | node=node->next; 24 | } 25 | } 26 | int main() 27 | { 28 | Node*head=NULL; 29 | push(&head,3); 30 | push(&head,4); 31 | push(&head,5); 32 | push(&head,6); 33 | push(&head,7); 34 | push(&head,8); 35 | printlist(head); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Linked_List/insertion_at_front.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/insertion_at_front.exe -------------------------------------------------------------------------------- /Linked_List/insertion_at_node.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/insertion_at_node.exe -------------------------------------------------------------------------------- /Linked_List/intersection_of_two_sorted_ll.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/intersection_of_two_sorted_ll.exe -------------------------------------------------------------------------------- /Linked_List/length_of_loop.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/length_of_loop.exe -------------------------------------------------------------------------------- /Linked_List/linked_list_palindrome.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/linked_list_palindrome.exe -------------------------------------------------------------------------------- /Linked_List/middle_of_Linkedlist.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/middle_of_Linkedlist.exe -------------------------------------------------------------------------------- /Linked_List/middle_of_ll_method2.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/middle_of_ll_method2.exe -------------------------------------------------------------------------------- /Linked_List/move_last_to_front.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/move_last_to_front.exe -------------------------------------------------------------------------------- /Linked_List/reverse.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/reverse.exe -------------------------------------------------------------------------------- /Linked_List/swap_without.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Linked_List/swap_without.exe -------------------------------------------------------------------------------- /NEW_Graphs/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/NEW_Graphs/output.txt -------------------------------------------------------------------------------- /NEW_Graphs/sanket_singh_bfs1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/NEW_Graphs/sanket_singh_bfs1.exe -------------------------------------------------------------------------------- /Recursion/recursion1/arrayissorted.cpp: -------------------------------------------------------------------------------- 1 | // problem to check if the array is sorted recursively 2 | #include 3 | using namespace std; 4 | bool issorted(int arr[], int n) 5 | { 6 | if(n==0 or n==1) 7 | { 8 | return true; 9 | } 10 | if((arr[0] < arr[1]) and issorted(arr+1,n-1)); 11 | { 12 | return true; 13 | } 14 | return false; 15 | } 16 | int main() 17 | { 18 | int arr[]={1,22,3,4,5}; 19 | int n=sizeof(arr)/sizeof(arr[0]); 20 | // if(issorted(arr,n)) 21 | // { 22 | // cout<<"Yes"; 23 | // }else{ 24 | // cout<<"No"; 25 | // } 26 | cout< 2 | using namespace std; 3 | 4 | int factorial(int n) 5 | { // base case 6 | if(n==0 ) 7 | { 8 | return 1; 9 | } 10 | // recursive case 11 | // int smallans = factorial(n-1); 12 | // int ans= n* smallans; 13 | 14 | // return ans; 15 | return n*factorial(n-1); 16 | 17 | } 18 | int main() 19 | { 20 | int n; 21 | cin>>n; 22 | int ans = factorial(n); 23 | cout< 2 | using namespace std; 3 | // fib no depends upon its previous two numbers 4 | int nfib(int n) 5 | { 6 | //base case 7 | if(n== 0 or n==1) 8 | { 9 | return n; 10 | 11 | } 12 | //recursive case 13 | // int f1= nfib(n-1); 14 | // int f2 = nfib(n-2); 15 | // return f1+f2; 16 | return nfib(n-1) + nfib(n-2); 17 | } 18 | 19 | int main() 20 | { 21 | int n; 22 | cin>>n; 23 | cout< 6 | using namespace std; 7 | 8 | void dec(int n) 9 | { 10 | if(n==0){ 11 | return; 12 | } 13 | cout<>n; 30 | dec(n); 31 | cout< 2 | using namespace std; 3 | 4 | int mul(int a,int b) 5 | { 6 | if(b==0) return 0; 7 | 8 | if(b>0) { 9 | return (a + mul(a,b-1));} 10 | } 11 | int main() 12 | { 13 | int a,b; 14 | cin>>a>>b; 15 | cout< 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | double myPow(double x, int n) 7 | { 8 | if(n==0) 9 | return 1; 10 | 11 | if(n%2==0) 12 | { 13 | if(n<0) 14 | { 15 | myPow((1/x)*(1/x), abs(n/2)); 16 | }else{ 17 | myPow(x*x,n/2); 18 | } 19 | }else{ 20 | if(n<0) 21 | { 22 | (1/x)*myPow((1/x)*(1/x), abs(n/2)); 23 | }else 24 | { 25 | x*myPow(x*x, abs(n/2)); 26 | } 27 | } 28 | } 29 | }; -------------------------------------------------------------------------------- /Recursion/recursion1/poweran.cpp: -------------------------------------------------------------------------------- 1 | //power(a,n) 2 | #include 3 | using namespace std; 4 | 5 | int power(int a,int n) 6 | { 7 | if(n==0) return 1; 8 | if(n==1) return a; 9 | else return a*power(a,n-1); 10 | } 11 | int main() 12 | { 13 | int a; 14 | cin>>a; 15 | int n; 16 | cin>>n; 17 | cout< 2 | using namespace std; 3 | 4 | int storeocc(int *arr, int i, int n, int key, int *ouput, int j) 5 | { 6 | if(i==n) return j+1; 7 | 8 | 9 | } 10 | 11 | void allocc(int *arr, int n, int i, int key) 12 | { 13 | if(i==n) return; 14 | if(arr[i]==key) cout< 2 | using namespace std; 3 | 4 | 5 | 6 | // a^n -> (a^n/2)^2 n is even 7 | // a^n -> a( a^n/2)^2 n is odd 8 | 9 | // a ^ 9= a(a^4)^2 10 | // (a^2)^2 11 | // (a^1)^2 12 | // a(a^0)^2 13 | // log n 14 | 15 | 16 | 17 | int fastpower(int a, int n) 18 | { 19 | if(n==0) 20 | { 21 | return 1; 22 | } 23 | int subprob = fastpower(a,n/2); 24 | subprob*=subprob; 25 | if( n&1) // odd 26 | { 27 | return subprob*a; 28 | } 29 | return subprob; 30 | // log n 31 | 32 | } 33 | 34 | 35 | int main() 36 | { 37 | int a,n; 38 | cin>>a>>n; 39 | 40 | cout< 2 | using namespace std; 3 | 4 | int lastocc(int arr[], int n, int key) 5 | { 6 | // base case 7 | if(n==0) return -1; 8 | int i =lastocc(arr+1,n-1,key); 9 | if(i==-1) 10 | { 11 | if(arr[0]==key) 12 | { 13 | return 0; 14 | } 15 | else{ 16 | return -1; 17 | } 18 | } 19 | return i+1; 20 | } 21 | int main() 22 | { 23 | int arr[]={1,2,3,7,4,6,7,10}; 24 | int n=sizeof(arr)/sizeof(int); 25 | int key=7; 26 | cout< 19 | using namespace std; 20 | 21 | int bikes(int n) 22 | { 23 | if(n==0) return 1; 24 | if(n==1) return 1; 25 | return bikes(n-1)+(bikes(n-2)*(n)); 26 | } 27 | int main() 28 | { 29 | int n; 30 | cin>>n; 31 | cout<< bikes(n); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Recursion/recursion3/friends_pairing.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Recursion/recursion3/friends_pairing.exe -------------------------------------------------------------------------------- /Recursion/recursion3/friends_pairing_problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int countpairs(int n) 5 | { 6 | // base condition 7 | if(n==0 || n==1 || n==2) return n; 8 | return countpairs(n-1) + (n-1)*countpairs(n-2); 9 | 10 | } 11 | 12 | int main() 13 | { 14 | int n=4; 15 | cout< 2 | using namespace std; 3 | 4 | int profit(int n, int c, int*wt, int *prices) 5 | { 6 | if(n==0 || c==0) return 0; 7 | 8 | int ans = 0; 9 | int inc, exc; 10 | inc =exc= INT_MIN; 11 | if(wt[n-1] <= c) 12 | { 13 | inc = prices[n-1] + profit( n-1, c - wt[n-1], wt,prices); 14 | } 15 | exc = profit(n-1, c,wt,prices); 16 | ans= max(inc, exc); 17 | 18 | return ans; 19 | 20 | 21 | 22 | } 23 | int main() 24 | { 25 | int weights[]={1,2,3,5}; 26 | int prices[]={40,20,30,100}; 27 | 28 | int n=4; 29 | int c=7; 30 | cout<< profit(n,c,weights,prices); 31 | 32 | 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Recursion/recursion3/knapsack_rec.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Recursion/recursion3/knapsack_rec.exe -------------------------------------------------------------------------------- /Recursion/recursion3/ladder_problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ladder(int n, int k) 5 | { 6 | if(n==0) return 1; 7 | if(n<0) return 0; 8 | 9 | int ans=0; 10 | for(int i=1;i<=k;i++) 11 | { 12 | ans+=ladder(n-i,k); 13 | } 14 | return ans; 15 | 16 | } 17 | int main() 18 | { 19 | int n, k; 20 | cin>>n>>k; 21 | cout< 2 | using namespace std; 3 | 4 | // problem 5 | // number to spelling 6 | // 2048 --> two zero four eight 7 | 8 | char words[][10]={"zero","one","two","three", "four", " five","six","seven","eight","nine"}; 9 | void printspellings(int n) 10 | { 11 | // base case 12 | if(n==0) 13 | { 14 | return; 15 | } 16 | // recur case 17 | printspellings(n/10); 18 | 19 | int digit = n%10; 20 | cout<< words[digit]<<" "; 21 | return; 22 | 23 | 24 | } 25 | 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | printspellings(n); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Recursion/recursion3/numbertospelling.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Recursion/recursion3/numbertospelling.exe -------------------------------------------------------------------------------- /Recursion/recursion3/phone_keypad.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Recursion/recursion3/phone_keypad.exe -------------------------------------------------------------------------------- /Recursion/recursion3/stringgeneration.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Recursion/recursion3/stringgeneration.exe -------------------------------------------------------------------------------- /Recursion/recursion3/subsequence_generation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void generatesub(char *in , char*out, int i, int j) 5 | { 6 | if(in[i]=='\0') 7 | { 8 | out[j]='\0'; 9 | cout< 2 | using namespace std; 3 | int gettile(int n) 4 | { 5 | if(n==0) return 0; 6 | if(n==1) return 1; 7 | return gettile(n-1) + gettile(n-2); 8 | } 9 | int main() 10 | { 11 | cout< 2 | using namespace std; 3 | 4 | 5 | void move(int n, char source, char helper, char dest) 6 | { 7 | // base case 8 | if(n==0) return; 9 | 10 | //rec case 11 | // move n-1 disk from src to helper 12 | move(n-1, source, dest, helper); 13 | cout<<"Shift disk "<>n; 22 | move(n ,'A','B','C'); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Recursion/recursion3/tower_of_hanoi.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/Recursion/recursion3/tower_of_hanoi.exe -------------------------------------------------------------------------------- /Tree_rev/levelorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left; 7 | Node*right; 8 | Node(int val) 9 | { 10 | data=val; 11 | left=NULL; 12 | right=NULL; 13 | } 14 | 15 | }; 16 | 17 | 18 | /* 19 | 1 20 | / \ 21 | 2 3 22 | / \ / \ 23 | 5 6 7 8 24 | */ 25 | 26 | void levelorder(Node*root) 27 | { 28 | 29 | } -------------------------------------------------------------------------------- /advanced_backtracking/n_queen.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/advanced_backtracking/n_queen.exe -------------------------------------------------------------------------------- /advanced_backtracking/n_queen_bitset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bitset<30> col,d1,d2; 6 | 7 | void solve(int r, int n, int &ans) 8 | { 9 | if(r==n) 10 | { 11 | ans++; // count the number of configuration 12 | return; 13 | } 14 | for(int c=0;c>n; 29 | int ans=0; 30 | solve(0,n,ans); 31 | cout< 2 | using namespace std; 3 | 4 | int n; 5 | int ans=0,DONE; 6 | void solve(int rowmask, int ld, int rd) 7 | { 8 | if(rowmask == DONE) { ans++; return;} 9 | 10 | int safe = DONE & (~( rowmask | ld |rd)); 11 | while(safe) 12 | { 13 | int p = safe & (-safe); 14 | safe = safe - p; 15 | solve(rowmask | p , (ld | p)<<1, (rd| p) >>1); 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | cin>>n; 22 | DONE =((1< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void helper(vector &ans,int n,int o=0,int c=0,string curr=""){ 7 | if(size(curr)==2*n){ 8 | ans.push_back(curr); 9 | return; 10 | } 11 | if(o generateParenthesis(int n) { 20 | vector ans; 21 | helper(ans,n); 22 | return ans; 23 | } 24 | }; -------------------------------------------------------------------------------- /backtracking/generate_parenthesis.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/backtracking/generate_parenthesis.exe -------------------------------------------------------------------------------- /backtracking/n_queen_10.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/backtracking/n_queen_10.exe -------------------------------------------------------------------------------- /backtracking/n_queen_problem.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/backtracking/n_queen_problem.exe -------------------------------------------------------------------------------- /backtracking/permutations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void permute(string s, string answer) 5 | { 6 | if(s.length()==0) 7 | { 8 | cout<>s; 29 | 30 | cout<<" \n all possible permutations are : "; 31 | permute(s,answer); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /backtracking/permutations.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/backtracking/permutations.exe -------------------------------------------------------------------------------- /backtracking/rat_in_a_maze.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/backtracking/rat_in_a_maze.exe -------------------------------------------------------------------------------- /backtracking/sudoku_solver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/backtracking/sudoku_solver.exe -------------------------------------------------------------------------------- /binary_tree/LCA_bt.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/LCA_bt.exe -------------------------------------------------------------------------------- /binary_tree/Mirror.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node*left; 8 | Node*right; 9 | }; 10 | 11 | 12 | void mir(Node*root) 13 | { 14 | if(!root) return ; 15 | mir(root->left); 16 | mir(root->right); 17 | swap(root->left, root->right); 18 | 19 | 20 | 21 | 22 | } -------------------------------------------------------------------------------- /binary_tree/Sum_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | int solve(Node*root) 9 | { 10 | if(!root) return; 11 | int a= solve(root->left); 12 | int b= solve(root->right); 13 | int x= root->data; 14 | root->data=a+b; 15 | return a+b+x; 16 | 17 | 18 | } 19 | void sumtree(Node*node) 20 | { 21 | solve(node); 22 | } -------------------------------------------------------------------------------- /binary_tree/array_implementation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/array_implementation.exe -------------------------------------------------------------------------------- /binary_tree/balanced.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | int f=1; 9 | 10 | int solve(Node*root) 11 | { 12 | if(!root) return 0; 13 | int l=solve(root->left); 14 | int r=solve(root->right); 15 | if(abs(l-r)>1) f=0; 16 | 17 | return max(l,r)+1; 18 | } 19 | 20 | bool isbalanced(Node*root) 21 | { 22 | f=1; 23 | solve(root); 24 | return f; 25 | } -------------------------------------------------------------------------------- /binary_tree/balanced_tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | int f=1; 10 | 11 | int solve(Node*root) 12 | { 13 | if(!root) return 0; 14 | int l=solve(root->left); 15 | int r=solve(root->right); 16 | if(abs(l-r)>1) f=0; 17 | return max(l,r)+1; 18 | 19 | } 20 | 21 | bool isbalanced(Node*root) 22 | { 23 | f=1; 24 | solve(root); 25 | return f; 26 | 27 | } -------------------------------------------------------------------------------- /binary_tree/binary_to_doubly_linked_list.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/binary_to_doubly_linked_list.exe -------------------------------------------------------------------------------- /binary_tree/bottom_view.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | vectorbottom(Node*root) 10 | { 11 | mapm; 12 | queue>q; 13 | vectorans; 14 | if(!q.empty()) return ans; 15 | q.push({root, 0}); 16 | while(!q.empty()) 17 | { 18 | Node*temp=q.front().first; 19 | int h=q.front().second; 20 | 21 | q.pop(); 22 | m[h]=temp->data; 23 | if(temp->left) q.push({root->left, h-1}); 24 | if(temp->right) q.push({root->right, h+1}); 25 | } 26 | for(auto x: m)ans.push_back(x.second); 27 | return ans; 28 | } -------------------------------------------------------------------------------- /binary_tree/bottomview.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node*left,*right; 8 | }; 9 | 10 | vector bottomview(Node*root) 11 | { 12 | mapm; 13 | vectorv; 14 | queue>q; 15 | if(!root) return v; 16 | q.push({root,0}); 17 | while(!q.empty()) 18 | { 19 | Node*temp=q.front().first; 20 | int h=q.front().second; 21 | q.pop(); 22 | m[h]=temp->data; 23 | if(temp->left) q.push({temp->left, h-1}); 24 | if(temp->right) q.push({temp->right, h+1}); 25 | 26 | } 27 | for(auto x: m) v.push_back(x.second); 28 | 29 | 30 | return v; 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /binary_tree/boundary_traversal_rev.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/boundary_traversal_rev.exe -------------------------------------------------------------------------------- /binary_tree/check_if_bt_is_sumtree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | int f=1; 9 | 10 | int solve(Node*root) 11 | { 12 | if(!root) return 0; 13 | if(!root->left and !root->right) return root->data; 14 | if(f==0) return 0; 15 | int a= solve(root->left); 16 | int b= solve(root->right); 17 | if(a+b != root->data) f=0; 18 | return a+b+root->data; 19 | 20 | } 21 | 22 | bool issumtree(Node*root) 23 | { 24 | f=1; 25 | solve(root); 26 | return f; 27 | 28 | } -------------------------------------------------------------------------------- /binary_tree/diagonal_trav_rev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | 10 | vectordiagonal(Node*root) 11 | { 12 | vectorans; 13 | queueq; 14 | 15 | if(!root) return ans; 16 | q.push(root); 17 | if(!q.empty()) 18 | { 19 | Node*temp=q.front(); 20 | q.pop(); 21 | while(temp) 22 | { 23 | if(temp->left) q.push(temp->left); 24 | ans.push_back(temp->data); 25 | temp=temp->right; 26 | } 27 | 28 | } 29 | return ans; 30 | 31 | } -------------------------------------------------------------------------------- /binary_tree/diagonal_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | vectordiagonal(Node*root) 10 | { 11 | queueq; 12 | vectorans; 13 | if(!root) return ans; 14 | 15 | q.push(root); 16 | while(!q.empty()) 17 | { 18 | Node*temp=q.front(); 19 | q.pop(); 20 | while(temp) 21 | { 22 | if(temp->left) q.push(temp->left); 23 | ans.push_back(temp->data); 24 | temp=temp->right; 25 | } 26 | } 27 | return ans; 28 | } -------------------------------------------------------------------------------- /binary_tree/diameter.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/diameter.exe -------------------------------------------------------------------------------- /binary_tree/diameter_rev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | Node*newnode(int data) 10 | { 11 | Node*newnode= new Node(); 12 | newnode->data=data; 13 | newnode->left=newnode->right=NULL; 14 | return newnode; 15 | } 16 | 17 | int ma; 18 | int solve(Node*root) 19 | { 20 | if(!root) return 0; 21 | int x=solve(root->left); 22 | int y=solve(root->right); 23 | 24 | ma=max(ma, x+y+1); 25 | return (max(x,y)+1); 26 | } 27 | 28 | int diam(Node*root) 29 | { 30 | ma=INT_MIN; 31 | int x=solve(root); 32 | return ma; 33 | } -------------------------------------------------------------------------------- /binary_tree/dist_bw_two_nodes_in_bt.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/dist_bw_two_nodes_in_bt.cpp -------------------------------------------------------------------------------- /binary_tree/dupliactes_subtree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Node 5 | { 6 | char data; 7 | struct Node* left; 8 | struct Node* right; 9 | }; 10 | unordered_mapm; 11 | string solve(Node*root) 12 | { 13 | if(!root) return "$"; 14 | string s=""; 15 | if(!root->right and !root->left){ 16 | s=to_string(root->data); 17 | return s; 18 | } 19 | s=s+to_string(root->data); 20 | s=s+solve(root->left); 21 | s=s+solve(root->right); 22 | m[s]++; 23 | return s; 24 | 25 | } 26 | bool dupSub(Node *root) 27 | { 28 | //your code here 29 | m.clear(); 30 | solve(root); 31 | for(auto x:m) 32 | { 33 | if(x.second >= 2) return true; 34 | } 35 | return false; 36 | } -------------------------------------------------------------------------------- /binary_tree/height_iterative.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/height_iterative.exe -------------------------------------------------------------------------------- /binary_tree/height_recursively.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/height_recursively.exe -------------------------------------------------------------------------------- /binary_tree/imsertion.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/imsertion.exe -------------------------------------------------------------------------------- /binary_tree/insertion.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/insertion.exe -------------------------------------------------------------------------------- /binary_tree/intro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | struct node*left; 7 | struct node*right; 8 | 9 | node(int val) 10 | { 11 | data=val; 12 | left=NULL; 13 | right=NULL; 14 | } 15 | }; 16 | int main() 17 | { 18 | struct node*root=new node(1); 19 | root->left=new node(2); 20 | root->right=new node(3); 21 | root->left->left=new node(4); 22 | root->data=NULL; 23 | 24 | while(root!=NULL) 25 | { 26 | cout<<"tree"<data<<" "; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /binary_tree/intro.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/intro.exe -------------------------------------------------------------------------------- /binary_tree/inverttree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/inverttree.cpp -------------------------------------------------------------------------------- /binary_tree/iterative_inorder.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/iterative_inorder.exe -------------------------------------------------------------------------------- /binary_tree/largest_subtreee_sum.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/largest_subtreee_sum.exe -------------------------------------------------------------------------------- /binary_tree/left_view.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/left_view.exe -------------------------------------------------------------------------------- /binary_tree/left_view_rev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node*left; 8 | Node*right; 9 | }; 10 | 11 | 12 | vectorleft(Node*root) 13 | { 14 | vectorans; 15 | queueq; 16 | if(!root) return ans; 17 | q.push(root); 18 | while(!q.empty()) 19 | { 20 | int sz=q.size(); 21 | ans.push_back(q.front()->data); 22 | while(sz--) 23 | { 24 | Node*temp=q.front(); 25 | q.pop(); 26 | if(temp->left) q.push(temp->left); 27 | if(temp->right) q.push(temp->right); 28 | 29 | 30 | } 31 | } 32 | return ans; 33 | } -------------------------------------------------------------------------------- /binary_tree/level_order_traversal.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/level_order_traversal.exe -------------------------------------------------------------------------------- /binary_tree/levelorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class Node{ 6 | public: 7 | int data; 8 | Node*left,*right; 9 | }; 10 | 11 | 12 | vectorlevel(Node*root) 13 | { 14 | vectorans; 15 | queueq; 16 | if(!root) return ans; 17 | 18 | q.push(root); 19 | while(!q.empty()) 20 | { 21 | Node*temp= q.front(); 22 | 23 | ans.push_back(temp->data); 24 | if(temp->left)q.push(temp->left); 25 | if(temp->right)q.push(temp->right); 26 | q.pop(); 27 | 28 | } 29 | return ans; 30 | } -------------------------------------------------------------------------------- /binary_tree/max_sum_no_two_adjacent.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/max_sum_no_two_adjacent.exe -------------------------------------------------------------------------------- /binary_tree/maximum_depth.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/maximum_depth.cpp -------------------------------------------------------------------------------- /binary_tree/mirrortree.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/mirrortree.exe -------------------------------------------------------------------------------- /binary_tree/print_all_k_sum_paths.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/print_all_k_sum_paths.exe -------------------------------------------------------------------------------- /binary_tree/reverse_level_order_trav.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/reverse_level_order_trav.exe -------------------------------------------------------------------------------- /binary_tree/reverse_level_order_traversal.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/reverse_level_order_traversal.exe -------------------------------------------------------------------------------- /binary_tree/right_view.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | 10 | 11 | vectorright(Node*root) 12 | { 13 | queueq; 14 | vectorans; 15 | if(!root) return ans; 16 | q.push(root); 17 | while(!q.empty()) 18 | { 19 | int sz=q.size(); 20 | Node*temp; 21 | while(sz--) 22 | { 23 | temp=q.front(); 24 | q.pop(); 25 | if(temp->left) q.push(temp->left); 26 | if(temp->right) q.push(temp->right); 27 | } 28 | ans.push_back(temp->data); 29 | } 30 | return ans; 31 | } 32 | -------------------------------------------------------------------------------- /binary_tree/sum_of_node_on_longest_path.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/binary_tree/sum_of_node_on_longest_path.exe -------------------------------------------------------------------------------- /binary_tree/sum_tree_rev.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | int solve(Node*root) 10 | { 11 | if(!root) return 0; 12 | int a=solve(root->left); 13 | int b=solve(root->right); 14 | int x=root->data; 15 | 16 | root->data=a+b; 17 | return a+b+x; 18 | } 19 | 20 | void solvefin(Node*root) 21 | { 22 | solve(root); 23 | } -------------------------------------------------------------------------------- /binary_tree/top_view.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Node{ 4 | public: 5 | int data; 6 | Node*left,*right; 7 | }; 8 | 9 | 10 | void topview(Node*root) 11 | { 12 | unordered_mapm; 13 | queue>q; 14 | if(!root) return; 15 | 16 | q.push({root,0}); 17 | 18 | while(!q.empty()) 19 | { 20 | Node*temp=q.front().first; 21 | int h=q.front().second; 22 | q.pop(); 23 | if(!m[h]) m[h]=temp->data; 24 | if(temp->left) q.push({temp->left, h-1}); 25 | if(temp->right) q.push({temp->right, h+1}); 26 | 27 | 28 | 29 | 30 | } 31 | for(auto x:m) cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node*left,*right; 8 | }; 9 | 10 | vector bottomview(Node*root) 11 | { 12 | mapm; 13 | vectorv; 14 | queue>q; 15 | if(!root) return v; 16 | q.push({root,0}); 17 | while(!q.empty()) 18 | { 19 | Node*temp=q.front().first; 20 | int h=q.front().second; 21 | q.pop(); 22 | if(!m[h]) m[h]=temp->data; 23 | if(temp->left) q.push({temp->left, h-1}); 24 | if(temp->right) q.push({temp->right, h+1}); 25 | 26 | } 27 | for(auto x: m) v.push_back(x.second); 28 | 29 | 30 | return v; 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /bit_manipulation/basics_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | unsigned char a=5, b=9; 6 | printf("a = %d, b = %d\n", a, b); 7 | printf("a&b = %d\n", a & b); 8 | 9 | // The result is 00001101 10 | printf("a|b = %d\n", a | b); 11 | 12 | // The result is 00001100 13 | printf("a^b = %d\n", a ^ b); 14 | 15 | // The result is 11111010 16 | printf("~a = %d\n", a = ~a); 17 | 18 | // The result is 00010010 19 | printf("b<<1 = %d\n", b << 1); 20 | 21 | // The result is 00000100 22 | printf("b>>1 = %d\n", b >> 1); 23 | 24 | return 0; 25 | 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /bit_manipulation/basics_1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/bit_manipulation/basics_1.exe -------------------------------------------------------------------------------- /bit_manipulation/xor_1.cpp: -------------------------------------------------------------------------------- 1 | // function returning only odd occuring element 2 | #include 3 | using namespace std; 4 | int findodd(int arr[], int n) 5 | { 6 | int res=0,i; 7 | for(i=0;i 5 | using namespace std; 6 | // int main() 7 | // { 8 | // int x=10, y=5; 9 | // x=x+y; 10 | // y=x-y; 11 | // x=x-y; 12 | // cout< 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n; 10 | cin>>n; 11 | char a[1000]; 12 | char largest[1000]; 13 | int len=0; 14 | int largestlen=0; 15 | cin.get(); // imp 16 | for(int i=0;i largestlen) 22 | { 23 | largestlen =len; 24 | strcpy(largest,a); 25 | } 26 | } 27 | cout< 2 | using namespace std; 3 | 4 | bool ispal(char a[]) 5 | { 6 | int i=0; 7 | int j= strlen(a)-1; 8 | 9 | while(i coding 3 | #include 4 | using namespace std; 5 | 6 | void removedup(char a[]) 7 | { 8 | 9 | int i=0; 10 | int j=i+1; 11 | int l = strlen(a); 12 | if( l ==1 or l==0) return; 13 | 14 | int prev=0; 15 | for(int curr =1;curr 2 | #include 3 | #include 4 | using namespace std; 5 | #define pb push_back 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | int n; 14 | cin>>n; 15 | vector dirn, roads; 16 | for(int i=0;i>rt_lft>>on; 20 | 21 | getline(cin,name); 22 | 23 | } 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /codechef_questions/ques2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define long long int int 4 | 5 | int main() { 6 | // your code goes here 7 | int t; 8 | cin>>t; 9 | while(t--) 10 | { 11 | int u,v,a,s; 12 | cin>>u>>v>>a>>s; 13 | int res; 14 | if(u <= v) 15 | { 16 | cout<<"Yes"< 6 | using namespace std; 7 | #define int long long 8 | int32_t main() { 9 | int t; 10 | cin>>t; 11 | while(t--) 12 | { 13 | int a,y,x; 14 | cin>>a>>y>>x; 15 | 16 | if(a>=y) 17 | { 18 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | dequedq; 7 | 8 | dq.push_back(5); 9 | dq.push_back(7); 10 | dq.push_front(6); 11 | dq.push_front(8); 12 | for(auto i :dq) 13 | { 14 | cout< 2 | using namespace std; 3 | 4 | class graph{ 5 | int v; 6 | list*adj; 7 | void dfsutil(int v, bool visited[]); 8 | 9 | public: 10 | graph(int v); 11 | void addedge(int v, int w); 12 | void dfs(int v); 13 | 14 | }; 15 | 16 | graph :: graph(int v) 17 | { 18 | this->v=v; 19 | adj=new list[v]; 20 | 21 | } -------------------------------------------------------------------------------- /graphs/dfs_stl.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/dfs_stl.exe -------------------------------------------------------------------------------- /graphs/dijkstra_pq_ajlist.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/dijkstra_pq_ajlist.exe -------------------------------------------------------------------------------- /graphs/dijsktra_algo.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/dijsktra_algo.exe -------------------------------------------------------------------------------- /graphs/dtect_cycle_colors.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/dtect_cycle_colors.exe -------------------------------------------------------------------------------- /graphs/intro.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void addEdge(vectoradj[], int u, int v) 4 | { 5 | adj[u].push_back(v); 6 | adj[v].push_back(u); 7 | } 8 | void printGraph(vectoradj[], int v) 9 | { 10 | for(int i=0;i"< adj[m]; 24 | addEdge(adj, 0, 1); 25 | addEdge(adj, 0, 4); 26 | addEdge(adj, 1, 2); 27 | addEdge(adj, 1, 3); 28 | addEdge(adj, 1, 4); 29 | addEdge(adj, 2, 3); 30 | addEdge(adj, 3, 4); 31 | printGraph(adj, m); 32 | return 0; 33 | 34 | } -------------------------------------------------------------------------------- /graphs/intro.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/intro.exe -------------------------------------------------------------------------------- /graphs/khan_topo_sort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/khan_topo_sort.exe -------------------------------------------------------------------------------- /graphs/mother_vertex.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/mother_vertex.exe -------------------------------------------------------------------------------- /graphs/mothervertex.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/mothervertex.exe -------------------------------------------------------------------------------- /graphs/number_of_island.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/number_of_island.exe -------------------------------------------------------------------------------- /graphs/prism.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/prism.exe -------------------------------------------------------------------------------- /graphs/set_implementation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/set_implementation.exe -------------------------------------------------------------------------------- /graphs/topologicalsort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/graphs/topologicalsort.exe -------------------------------------------------------------------------------- /hashing/another_arraysubset.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int arr1[]={ 11, 1, 13, 21, 3, 7 }; 7 | int arr2[] = { 11, 3, 7, 1 }; 8 | int m = sizeof(arr1) / sizeof(arr1[0]); 9 | int n = sizeof(arr2) / sizeof(arr2[0]); 10 | unordered_set s; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool checkSubarraySum(vector& nums, int k) { 7 | unordered_map mp; 8 | int sum=0,i; 9 | mp[0]=-1; 10 | 11 | for(i=0;i=2) 19 | return true; 20 | } 21 | else 22 | { 23 | mp[key]=i; 24 | } 25 | 26 | } 27 | 28 | return false; 29 | } 30 | }; -------------------------------------------------------------------------------- /hashing/four_sum.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/hashing/four_sum.exe -------------------------------------------------------------------------------- /hashing/frequency_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void countfreq(int arr[], int n) 6 | { 7 | unordered_mapmap; 8 | for(int i=0;i" << cumulatives << endl; 20 | } 21 | map[arr[i]]=0; 22 | } 23 | } 24 | int main() 25 | { 26 | int a[] = {1, 3, 2, 4, 2, 1}; 27 | int n = sizeof(a)/sizeof(a[0]); 28 | countfreq(a, n); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /hashing/frequency_count.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/hashing/frequency_count.exe -------------------------------------------------------------------------------- /hashing/hashmapcustomclass.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/hashing/hashmapcustomclass.exe -------------------------------------------------------------------------------- /hashing/hashmapstl.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/hashing/hashmapstl.exe -------------------------------------------------------------------------------- /hashing/highest_lowest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int finddiff(int arr[], int n) 4 | { 5 | unordered_maphm; 6 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | int finlongest(int arr[], int n) 5 | { 6 | unordered_sets; 7 | int ans=0; 8 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int minoperations(int arr[], int n) 6 | { 7 | unordered_maphashed; 8 | 9 | for(int i=0;i 2 | // #include 3 | // using namespace std; 4 | 5 | // int calculate(int a[], int n) 6 | // { 7 | // int *maximum= max_element(a, a + n); 8 | // int frequency[*maximum + 1] = {0}; 9 | 10 | // for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | bool sortbyval(const pair&a, const pair&b) 6 | { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /hashing/subarray_0.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/hashing/subarray_0.exe -------------------------------------------------------------------------------- /hashing/subarraysumzero.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/hashing/subarraysumzero.exe -------------------------------------------------------------------------------- /hashing/sum_pair.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void printpairs(int arr[], int n , int sum) 5 | { 6 | unordered_sets; 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unordered_mapm; 7 | m.insert(make_pair("Mango",100)); 8 | pairp; 9 | p.first="Apple"; 10 | p.second=120; 11 | m.insert(p); 12 | 13 | m["Banana"]=20; 14 | 15 | for(auto it =m.begin();it!=m.end();it++) 16 | { 17 | cout<first<<" and "<second< 2 | using namespace std; 3 | class Solution { 4 | public: 5 | bool isAnagram(string s, string t) { 6 | int hsh1[26]={0}; 7 | int hsh2[26]={0}; 8 | 9 | for(int i=0;i 2 | using namespace std; 3 | int mincost(int arr[], int n) 4 | { 5 | priority_queue, greater>pq(arr,arr+n); 6 | int res=0; 7 | while(pq.size()>1) 8 | { 9 | int first =pq.top(); 10 | pq.pop(); 11 | int second = pq.top(); 12 | pq.pop(); 13 | 14 | res+=first+second; 15 | pq.push(first+second); 16 | } 17 | return res; 18 | } 19 | 20 | 21 | int main() 22 | { 23 | int len[] = { 4, 3, 2, 6 }; 24 | int size = sizeof(len) / sizeof(len[0]); 25 | cout << "Total cost for connecting ropes is " << mincost(len, size); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /heap/connect_n_ropes_with_min.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/heap/connect_n_ropes_with_min.exe -------------------------------------------------------------------------------- /heap/heap_from_scratch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Maxheap{ 5 | int *harr; // pointer to array 6 | int capacity; // max possible size 7 | int heap_size; //current number of elements in max heap 8 | 9 | 10 | public: 11 | Maxheap(int a[], int n); 12 | void maxheapify(int i); 13 | int parent(int i){ return (i-1)/2;} 14 | int left(int i){ 15 | return (2*i+1); 16 | } 17 | int right(int i) 18 | { 19 | return (2*i+2); 20 | } 21 | 22 | int extractmax(); 23 | int getmax(){return harr[0];} 24 | void replacemax(int x) 25 | { 26 | harr[0]=x; 27 | maxheapify(0); 28 | } 29 | 30 | }; 31 | -------------------------------------------------------------------------------- /heap/heapsort.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/heap/heapsort.exe -------------------------------------------------------------------------------- /heap/is_bt_is_heap.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/heap/is_bt_is_heap.exe -------------------------------------------------------------------------------- /heap/kth_largest_in_a_stream.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/heap/kth_largest_in_a_stream.exe -------------------------------------------------------------------------------- /heap/merge_k_sorted_arrays.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/heap/merge_k_sorted_arrays.exe -------------------------------------------------------------------------------- /heap/mergek_sorted_lists.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/heap/mergek_sorted_lists.exe -------------------------------------------------------------------------------- /heap/minheap_maxheap.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/heap/minheap_maxheap.exe -------------------------------------------------------------------------------- /interviewquestions/reverselinkedlist.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/interviewquestions/reverselinkedlist.cpp -------------------------------------------------------------------------------- /mock_practice.cpp/linked_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public: 6 | int data; 7 | node*next; 8 | }; 9 | void printlist(node*n) 10 | { 11 | while(n!=NULL) 12 | { 13 | cout<data<<" "; 14 | n=n->next; 15 | } 16 | } 17 | int main() 18 | { 19 | node*head=NULL; 20 | node*second=NULL; 21 | node*third=NULL; 22 | head = new node(); 23 | second = new node(); 24 | third = new node(); 25 | head->data =1; 26 | head->next=second; 27 | second->data=2; 28 | second->next=third; 29 | third->data=3; 30 | third->next=NULL; 31 | printlist(head); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /mock_practice.cpp/linked_list.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/mock_practice.cpp/linked_list.exe -------------------------------------------------------------------------------- /mock_practice.cpp/reverse_linkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class node{ 4 | public: 5 | int data; 6 | node*next; 7 | node(int data){ 8 | this->data=data; 9 | next=NULL; 10 | } 11 | }; 12 | 13 | struct Linkedlist{ 14 | node*head; 15 | Linkedlist() 16 | { 17 | head=NULL; 18 | } 19 | void reverse() 20 | { 21 | node*current=head; 22 | node*prev,*next=NULL; 23 | while(current!=NULL) 24 | { 25 | next=current->next; 26 | current->next=prev; 27 | prev=current; 28 | current=next; 29 | 30 | } 31 | head=prev; 32 | 33 | } 34 | }; 35 | void print() 36 | { 37 | 38 | } -------------------------------------------------------------------------------- /mock_practice.cpp/stack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/mock_practice.cpp/stack.exe -------------------------------------------------------------------------------- /mock_practice.cpp/stack_ll.cpp: -------------------------------------------------------------------------------- 1 | // #include 2 | // using namespace std; 3 | 4 | // class stacknode{ 5 | // public: 6 | // int data; 7 | // stacknode*next; 8 | // }; 9 | // stacknode* newnode(int data) 10 | // { 11 | // // stacknode* stacknode = new stacknode(); 12 | // // stacknode->data =data; 13 | // // stacknode->next=NULL; 14 | // // return stacknode; 15 | 16 | // // } 17 | // int isempty(stacknode*root) 18 | // { 19 | // return !root; 20 | // } -------------------------------------------------------------------------------- /number_theory/bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | cout<< INT_MAX<<"\n"; 6 | int a = (1LL<<31) -1; 7 | cout< 2 | using namespace std; 3 | 4 | void printb(int num) 5 | { 6 | for(int i=10;i>=0;i--) 7 | { 8 | cout<<((num>>i)&1); 9 | } 10 | cout< 2 | using namespace std; 3 | 4 | vector generate(int n) 5 | { 6 | vectorans; 7 | queueq; 8 | q.push("1"); 9 | for(int i=1;i<=n;i++ ) 10 | { 11 | string binary = q.front(); 12 | q.pop(); 13 | ans.push_back(binary); 14 | q.push(binary+"0"); 15 | q.push(binary+"1"); 16 | 17 | } 18 | return ans; 19 | } 20 | 21 | 22 | int main() 23 | { 24 | int n=5; 25 | vectorans = generate(n); 26 | for (auto i = ans.begin(); i != ans.end(); ++i) 27 | std::cout << *i << ' '; 28 | 29 | return 0; 30 | 31 | } -------------------------------------------------------------------------------- /queues/generate_binary_num.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/queues/generate_binary_num.exe -------------------------------------------------------------------------------- /queues/linked_list_impl.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/queues/linked_list_impl.exe -------------------------------------------------------------------------------- /queues/minheap_pqueue.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/queues/minheap_pqueue.exe -------------------------------------------------------------------------------- /queues/priority_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void show( priority_queuepq ) 5 | { 6 | priority_queue g= pq; 7 | while(!g.empty()) 8 | { 9 | cout<<'\t'< qp; 19 | qp.push(20); 20 | qp.push(90); 21 | qp.push(40); 22 | qp.push(10); 23 | qp.push(100); 24 | cout<<"priority queue is: "; 25 | show(qp); 26 | cout<<"size is "< 2 | using namespace std; 3 | 4 | void reverseq(queue&q) 5 | { 6 | stackst; 7 | while(!q.empty()) 8 | { 9 | st.push(q.front()); 10 | q.pop(); 11 | 12 | } 13 | 14 | while(!st.empty()) 15 | { 16 | q.push(st.top()); 17 | st.pop(); 18 | } 19 | } 20 | 21 | void printq(queue&q) 22 | { 23 | while(!q.empty()) 24 | { 25 | cout<q; 32 | q.push(1); 33 | q.push(2); 34 | q.push(3); 35 | q.push(4); 36 | q.push(5); 37 | 38 | reverseq(q); 39 | printq(q); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /queues/reverse_q.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/queues/reverse_q.exe -------------------------------------------------------------------------------- /queues/reversing_queue.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/queues/reversing_queue.exe -------------------------------------------------------------------------------- /searching/count_squares.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int squareans(int n) 4 | { 5 | int a=sqrt(n); 6 | if(a*a == n) 7 | { 8 | return a-1; 9 | 10 | }else{ 11 | return a; 12 | } 13 | } 14 | 15 | 16 | 17 | int main() 18 | { 19 | int n=23; 20 | int ans= squareans(n); 21 | cout< 2 | using namespace std; 3 | 4 | int binarys(int arr[], int low, int high) 5 | { 6 | while(low<=high) 7 | { 8 | int mid=(low+high)/2; 9 | if(arr[mid]==mid) 10 | { 11 | return mid; 12 | }else if(mid > arr[mid]) 13 | { 14 | return binarys(arr,(mid+1), high); 15 | }else{ 16 | return binarys(arr,low, mid-1); 17 | } 18 | } 19 | return -1; 20 | } 21 | 22 | int main() 23 | { 24 | int arr[]={-10,-5,0,3,7}; 25 | int n=sizeof(arr)/sizeof(arr[0]); 26 | int fixedpoint = binarys(arr,0,n-1); 27 | cout< 2 | using namespace std; 3 | 4 | // int majority(int arr[], int n) 5 | // { 6 | // int val=(n/2); 7 | // unordered_mapm; 8 | // for(int i=0;i val) return x.first; 15 | // } 16 | // return -1; 17 | // } 18 | 19 | int main() 20 | { 21 | int arr[]={3,3,4,2,4,4,2,4,4}; 22 | int n=sizeof(arr)/sizeof(arr[0]); 23 | // int majoritylele= majority(arr, n); 24 | // cout< 2 | using namespace std; 3 | int majority(int arr[], int n) 4 | { 5 | int major=arr[0]; 6 | int count=1; 7 | for(int i=1;i n/2) return major; 23 | else return -1; 24 | } 25 | 26 | 27 | 28 | int main() 29 | { 30 | int arr1[]={3,1,3,3,2,1,2,3}; 31 | int n= sizeof(arr1)/sizeof(arr1[0]); 32 | cout< 2 | using namespace std; 3 | 4 | 5 | int *findtwo(int *arr, int n) 6 | { 7 | int a,b; 8 | for(int i=0;i0) 21 | { 22 | b=i+1; 23 | break; 24 | } 25 | } 26 | int*v = new int[2]; 27 | v[0]=a; 28 | v[1]=b; 29 | return v; 30 | 31 | 32 | } 33 | int main() 34 | { 35 | int arr[]={4,3,6,2,1,1}; 36 | int n=sizeof(arr)/sizeof(arr[0]); 37 | 38 | 39 | 40 | 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /searching/sorted_rotated_array.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/searching/sorted_rotated_array.exe -------------------------------------------------------------------------------- /searching_sorting/Product_array_puzz.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/searching_sorting/Product_array_puzz.exe -------------------------------------------------------------------------------- /searching_sorting/binary_search.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashikajotwani12/DataStructures_Algorithms/6b82cc98f0ed4177915765de1b6beddad0b8af1d/searching_sorting/binary_search.exe -------------------------------------------------------------------------------- /searching_sorting/interpolation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int interpolation(int arr[], int l, int h, int x) 4 | { 5 | int pos; 6 | if(l<=h &&x>=arr[l] &&x<=arr[h]) 7 | { 8 | pos=l+() 9 | } 10 | } -------------------------------------------------------------------------------- /searching_sorting/linear_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int search(int arr[], int n, int x) 4 | { 5 | for(int i=0;i 2 | using namespace std; 3 | 4 | int minswaps(int arr[], int n) 5 | { 6 | vector>v(n); 7 | for(int i=0;i 2 | using namespace std; 3 | bool findpair(int arr[], int n, int diff) 4 | { 5 | int i=0; 6 | int j=1; 7 | while(i 2 | using namespace std; 3 | 4 | bool mycampare(pairp1, pairp2) 5 | { 6 | return p1.first < p2.first; 7 | 8 | } 9 | 10 | int main() 11 | { 12 | int arr[ ]={10,16,7,14,5,3,12,9}; 13 | int n=sizeof(arr)/sizeof(arr[0]); 14 | vector>v; 15 | for(int i=0;i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | vectorv; 7 | v.push_back(2); 8 | v.push_back(2); 9 | v.push_back(2); 10 | v.push_back(2); 11 | v.push_back(2); 12 | for(int i=0;i::iterator i; 18 | for(i=v.begin();i!=v.end();i++) 19 | { 20 | cout<<*i<<" "; 21 | } 22 | cout< 2 | using namespace std; 3 | 4 | void wavesort(int arr[], int n) 5 | { 6 | for(int i=1;i arr[i-1]) 9 | { 10 | swap(arr[i], arr[i-1]); 11 | } 12 | if(arr[i]> arr[i+1] and i<=n-2) 13 | { 14 | swap(arr[i], arr[i+1]); 15 | } 16 | 17 | } 18 | } 19 | 20 | 21 | int main() 22 | { 23 | int arr[]={1,3,4,7,5,6,2}; 24 | int n=sizeof(arr)/sizeof(arr[0]); 25 | wavesort(arr, n); 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | int matrix[3][3]={{0,1,0}, 5 | {0,0,0}, 6 | {0,1,0}}; 7 | 8 | int knows(int a, int b) 9 | { 10 | return matrix[a][b]; 11 | } 12 | int main() 13 | { 14 | int celeb=0; 15 | int n=3; 16 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | void reversesentence(string s) 5 | { 6 | stackst; 7 | 8 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | void calculatespan(int price[], int n, int s[]) 5 | { 6 | stackst; 7 | st.push(0); 8 | s[0]=1; 9 | for(int i=1;i 2 | using namespace std; 3 | 4 | void permute(string a , int l, int r) 5 | { 6 | if(l==r) 7 | cout< 2 | using namespace std; 3 | void reversed(string s) 4 | { 5 | vectortemp; 6 | string str=""; 7 | for(int i=0;i0 ; i--) 21 | { 22 | cout<