├── .vscode └── settings.json ├── 8_queens_problem.cpp ├── Algorithms ├── Aggressivecows(Binarysearch).cpp ├── BFS graph.c ├── BFS graph.cpp ├── Breadth First Search.cpp ├── CocktailSort ├── CocktailSort.cpp ├── DFS ├── Detecting a cycle in a graph (Using three color mechanism).cpp ├── Dijkstra_spf.cpp ├── Extended Euclid Algo.cpp ├── Graph │ ├── .cph │ │ └── .Kadane's_algorithm.cpp_1d25845e7d2ec1b81fac4c8baeb39b9b.prob │ ├── Bipartite_graph.cpp │ ├── Dijkstra’s_shortest_path_algorithm.cpp │ ├── Kadane's_algorithm.bin │ ├── Kadane's_algorithm.cpp │ ├── Trie.cpp │ ├── graph_dia_rad.py │ └── tarjan's algorithm.cpp ├── Huffman.cpp ├── HuffmanEncoder.java ├── KaratsubaAlgor.java ├── LCS.cpp ├── Lee.cpp ├── Math │ ├── AckermannFunction.cpp │ ├── BinaryExponentiation.cpp │ ├── Binomial Coefficient.cpp │ ├── CRT.cpp │ ├── CatalanNumbers.cpp │ ├── CollatzConjectureHailStoneSequence.py │ ├── Euclid euler.cpp │ ├── Euler_Totient_Function.cpp │ ├── FermatsLittleTheorem.cpp │ ├── Gould_sequence_dp.cpp │ ├── Matrix Exponentiation │ │ ├── Matrix Exponentiation.cbp │ │ ├── Matrix Exponentiation.layout │ │ ├── bin │ │ │ └── Debug │ │ │ │ └── Matrix Exponentiation.exe │ │ ├── main.cpp │ │ └── obj │ │ │ └── Debug │ │ │ └── main.o │ ├── NewtonsMethod.cpp │ ├── Numbers with exactly 3 divisors.cpp │ ├── NumericalDifferentiation.py │ ├── Prime_Factorization_Number_and_Sum_of_divisors.cpp │ ├── SieveOfEratosthenes.cpp │ ├── SmithNumbers.cpp │ ├── Strassen's_Matrix_Mul.py │ ├── binomial_coefficient_linear.cpp │ ├── exponentiation_ii_2_cses_calculate_a^b^c.cpp │ ├── golden_ratio.cpp │ ├── max_value_arithmetic_expressons │ └── segmented_seive.cpp ├── MatrixChainMultiplication.cpp ├── Merge_Sort.py ├── Min no of fountain to cover garden.cpp ├── MinimumCostPath.cpp ├── OS Algorithms │ ├── Banker's_algorithm.cpp │ ├── First_Come_First_Serve.c │ ├── Non-Preemptive_Priority.c │ ├── Preemptive_Priority.c │ ├── RoundRobin.c │ └── Shortest_Job_First.c ├── Pathfinder.cpp ├── Prims.cpp ├── QuicksortAlogorithm.js ├── Recursion │ └── Coin_Change_Recursion │ │ └── c++ │ │ ├── CoinChange.cpp │ │ ├── Egg_Dropping_Puzzle.cpp │ │ └── josephus_problem.cpp ├── RodCutting.cpp ├── Search_a_2D_Matrix.cpp ├── Selectionsort. Cpp ├── SieveOfEratosthenes.cpp ├── Strassensalgo.c ├── TSP Genetic.cpp ├── Topological.cpp ├── Uninformed-Search │ ├── a.out │ ├── bfs.cpp │ ├── dfs.cpp │ └── iddfs.cpp ├── a.out ├── backtracking.cpp ├── bellmanFord.cpp ├── binary_search.cpp ├── bomberman_game.cpp ├── equalizeArray.cpp ├── floyd.cpp ├── gcdEucidAlgo.cpp ├── heap_sort.c ├── kadane_algorithm.cpp ├── kruskals_algorithm.cpp ├── linearreg.py ├── manacher's algorihm ├── median_algorithm.py ├── mergesort.java ├── moore alogrithm │ └── moore alogirthm.cpp ├── multiplereg.py ├── n_queens_problem.cpp ├── optimal_merge_pattern.cpp ├── quick_sort.py ├── ratmaze.java ├── variable │ └── ChangesVariable.kt └── wave_rint.cpp ├── Arrays ├── Best_Time_To_Buy_Stocks.cpp ├── Check_Repeated_digits_using_arrays.cpp ├── Count of Smaller Numbers After Self.cpp ├── Find the missing number in a consecutive array list.cpp ├── FirstRepeatingElement.cpp ├── Intersection.c++ ├── Kadanes.java ├── Long_subarr_maxdiff_bwTwo_elmt.c ├── Long_subrr_Sum_elemt_k.cpp ├── Moore_Voting_Algorithm.cpp ├── Organising_Containers.c ├── PeakFinding.cpp ├── RemoveDuplicates.cpp ├── SecondLargestInArray.java ├── Sort-0-1-2.cpp ├── SpiralMatrix.java ├── Sub_Array_with_given_sum.cpp ├── Subarray XOR.cpp ├── Sum_of_numbers_in_array.cpp ├── TicTacToe.cpp ├── Trapping_Rain_Water.cpp ├── TwoSum.java ├── Union.c++ ├── a.out ├── anticlockwise_matrix_rotation.cpp ├── array_rotation.py ├── binary_Array.cpp ├── bookAllocation.cpp ├── catalan_numbers.cpp ├── clockwise_matrix_rotation.cpp ├── construct-binary-tree-from-preorder-and-inorder-traversal.py ├── conversion_number_System.cpp ├── conversion_to_different_number_system.cpp ├── create_spiral_mat.cpp ├── factorial.cpp ├── finding_dublicate.cpp ├── generic_fun.cpp ├── hotel_bookin_poss.cpp ├── inversion.cpp ├── kadane.cpp ├── kth_pascal_row.cpp ├── longest_range_ofelement_array.py ├── longestsubseq.cpp ├── magical_chessboard.cpp ├── matrix_multiplication.c ├── max_absolute_diff.cpp ├── max_distance.cpp ├── max_subarray_sum.js ├── max_sum_subarray.cpp ├── max_sum_subarray.py ├── max_unsorted_subarr.cpp ├── maxm_consequtive_gap.cpp ├── merge_overlapping_intervals.py ├── min-max-element-in-array.cpp ├── minmaxelementfinder.java ├── moore_voting_algo.cpp ├── negative_nos_at_one_end.cpp ├── next_permutation.cpp ├── noble_integer.cpp ├── nth_fibonacci.cpp ├── pairSum.cpp ├── partition.cpp ├── pascal_triangle.cpp ├── peak-element.cpp ├── prime_or_not.cpp ├── priority_queue.cpp ├── priority_queue_make_your_own.cpp ├── regex_pattern.cpp ├── removeDuplicates.py ├── reverse pairs.cpp ├── saddlePoint.java ├── spiral_matrix_traversal.cpp ├── stacks_implementation.cpp ├── subarraySum.cpp ├── subarray_with_given_sum.java ├── sublists_of_lists.py ├── ternary_search.cpp ├── transpose_array.cpp ├── triplet.cpp ├── twoNumberSum.py ├── vec_temp.cpp └── wave_array.cpp ├── Binary_Search_Tree ├── BalancedBST │ ├── BBST.cpp │ ├── Readme.md │ └── rightViewOfBST.cpp ├── Flattening of BST.cpp ├── Insert_node_iterative_method.cpp ├── LCA_on_BST │ ├── LCAonBST │ └── LCAonBST.cpp ├── Least_common_Anccestor_in_BST.cpp ├── Rotations in trees.c ├── VerticalOrderTraversal_BinaryTree.cpp ├── ZIGZAG_LEVEL_ORDER.cpp ├── binarytree.c ├── bstInNodeInRange.cpp ├── bst_in_python3.py ├── construct_BST_from_postOrder.cpp ├── floorAndCeilinBST.cpp ├── hight_of_BST.cpp ├── largestBSTinBT.java ├── level_order_travesal.cpp ├── max_and_min_element.cpp ├── min_ele_inBST.cpp ├── min_hight_of_BST.cpp ├── normal_BST_to_balanceBST.cpp ├── pair_sum_bst.cpp ├── sum_of_leaf_nodes_inBST.cpp └── validateBST.cpp ├── Binary_tree ├── BT_left_view.py ├── BinaryTree.java ├── Build-heightbalanced-BinaryTree │ ├── .DS_Store │ ├── buildHeightBalanced_binaryTree.cpp │ ├── inputf.in │ └── outputf.in ├── In Order Morris Traversal In Binarytree.cpp ├── InvertBinaryTree.cpp ├── Iterative_Postorder_Traversal.java ├── Right_view_BinaryTree.cpp ├── a.out ├── binary_heap.c ├── binary_tree.cpp ├── bottom_view.cpp ├── boundary_traversal.cpp ├── find_LCA_binary_tress.cpp ├── matrix.cpp ├── postOrder_traversal.cpp ├── tree_trvrsl.cpp └── vertical_traversal.cpp ├── CODECHEF EASY ├── ALTER.cpp ├── AMR15A.cpp ├── BUS.cpp ├── CHEFCAR.cpp ├── CHEFSUM.cpp ├── CHOPRT.cpp ├── CIELRCPT.cpp ├── DECINC.cpp ├── DIFFSUM.cpp ├── FCTRL2.py ├── FLOW001.cpp ├── FLOW002.cpp ├── FLOW004.cpp ├── FLOW005.cpp ├── FLOW007.cpp ├── FLOW008.cpp ├── FLOW009.cpp ├── FLOW010.cpp ├── FLOW013.cpp ├── FLOW016.cpp ├── FLOW017.cpp ├── FLOW018.cpp ├── FSQRT.cpp ├── INTEST.cpp ├── LAPIN.cpp ├── MUFFINS3.cpp ├── PALL01.cpp ├── PERMUT2.cpp ├── PPSUM.cpp ├── PRB01.cpp ├── REMISS.cpp ├── START01.cpp ├── TLG.cpp ├── TRISQ.cpp ├── TSORT.cpp ├── XORIER.cpp ├── template.cpp ├── test.cpp └── test.py ├── CONTRIBUTING.md ├── CSES problems └── Course schedule │ ├── problem.md │ └── solution.cpp ├── CodeForces Problems ├── 230_A.cpp ├── 330_A.cpp ├── 432_A.cpp ├── 443_A.cpp ├── 466_A.cpp ├── 58_A.cpp ├── CF-1385-B-Div_3-Restore the Permutation by Merger.cpp ├── CF-1399-A-Div_3-Remove Smallest.cpp ├── CF-1408-A-Circle Coloring.cpp ├── CF-635-B-Ichihime and Triangle.cpp ├── CF-749-B-Div2-Omkar_and_heavenly_tree.cpp ├── CF_146_A.cpp ├── CF_677_F.cpp ├── CF_744_A.cpp ├── CF_750_A.cpp ├── CF_791_A.c ├── C_Inverse_Of_Permutation ├── Codeforces_667_Div3 │ ├── A.cpp │ └── B.cpp ├── DoNotBeDistracted.java ├── Secret santa │ ├── problem.md │ └── solution.cpp ├── cf634_B.cpp ├── cf_633.cpp ├── cf_634_B.cpp ├── cf_636_A.cpp ├── cf_644_div3_H.cpp ├── cf_B_Decoding.cpp ├── cf_educational_97_C.cpp ├── cfdiv21605.cpp ├── cfdiv4.cpp ├── cfround636B.cpp ├── cfwatermelon.cpp ├── codeforces635_B.cpp └── sort_0_1_2.cpp ├── CodeVita ├── TCS_ CodeVita - Coding Arena.pdf ├── constellation.cpp ├── min_the_sum.py ├── paste_reduction.py ├── railway.c ├── railway.py ├── railway_station.cpp ├── test_cons.cpp └── travel_cost.cpp ├── Count of Smaller Numbers After Self.cpp ├── DP ├── 0-1 Knapsack │ ├── 0-1 Knapsack.cpp │ └── a.exe ├── 10.minimum subset sum differance.txt ├── 18. Friends Pairing.cpp ├── Best_Time_To_Sell_Stock.cpp ├── Binomialcoefficient │ ├── binomial.cpp │ ├── binomial1.cpp │ ├── binomial2.cpp │ ├── binomial3.cpp │ ├── binomial4.cpp │ └── binomial5.cpp ├── Coin_game_winner_where_every_player_has_three_choices.cpp ├── Dice_Throw.cpp ├── EGG Dropping Puzzle │ ├── ProblemSatement.txt │ ├── solution.c │ ├── solution.cpp │ ├── solution.java │ └── solution.py ├── Equal_Sum_partition │ ├── Equal sum partition.txt │ ├── equal_sum_parititon_memoization.cpp │ ├── equal_sum_partition_Recursive.cpp │ └── equal_sum_partition_bottom-up.cpp ├── Fibincci_series │ ├── Fibonacci.java │ ├── a.out │ ├── alterntive_formula_method.cpp │ ├── nth_fiboncci_series.cpp │ └── sum_of_nFibonacci_seq.cpp ├── Gold_Mine_Problem_Cpp │ └── gold_mine.cpp ├── House Robber │ ├── ProblemStatement.txt │ └── houserobber2.cpp ├── Interleaved Strings │ ├── InterleavedStrings.cpp │ └── InterleavedStrings.exe ├── Kadane-Algorithm │ └── Largest-Sum-Contiguous-Subarray.cpp ├── Knapsack-2 │ ├── problem.md │ └── solution.cpp ├── Longest Common Substring.cpp ├── Longest Increasing Path in Matrix │ └── 329-LongestIncreasingPathInMatrix.java ├── Longest Palindromic Substring │ ├── LongestPalindromicSubstring.cpp │ └── LongestPalindromicSubstring.exe ├── LongestIncreasingSubsequence_DP_APPROACH.cpp ├── Longest_Common_subsequence │ └── c++ │ │ └── lcs.cpp ├── Maximum_Score_From_performing_Multiplication.java ├── Maximum_Value_Contiguous_Subsequence │ ├── c++ │ │ └── MVCS.cpp │ └── python │ │ └── MVCS.py ├── Min_cost_for_ticket.java ├── MinimumSumPartition.cpp ├── Minimum_Coin_Change │ ├── MinimumCoinsExchangeUsingBottomUpApproach.cpp │ └── minCoinChange.py ├── Minimum_Multiplications_to_Reach_End.cpp ├── Multistage_Graph.cpp ├── Needleman-Wunsch │ ├── Needleman-Wunsch.cpp │ └── a.exe ├── NumberOfLongestIncreasingSubsequence.cpp ├── Nussinov Algorithm │ ├── Nussinov Algorithm.cpp │ └── a.exe ├── Palindrome Partitioning II.java ├── Palindrome Partitioning II │ └── palindrome partioning 2.java ├── Palindrome_partitioning │ └── palindrome_partitioning.cpp ├── Permutation Coefficient │ ├── a.out │ └── permutation_coefficient.cpp ├── Pots_of_Gold_Game.cpp ├── Recursive-Function │ └── Recursive function.cpp ├── Rod Cutting Problem │ └── C++ │ │ ├── RodCutDP │ │ └── RodCutDP.cpp ├── Rod-Cutting │ └── Rod-cutting_top-down_bottom-up.cpp ├── Segmented Least Squares │ ├── Segmented Least Squares.cpp │ └── a.exe ├── Shortest Common Supersequence │ ├── problemStatement.txt │ └── shortestCommonSuperSequence.cpp ├── Sticker theif problem │ ├── c++ solution.cpp │ └── problem statement ├── Subset-sum │ ├── DP table for example.txt │ ├── Problem_statement.txt │ └── Solution.cpp ├── Ugly_Number │ ├── a.out │ └── find_nth_ugly_number.cpp ├── Unbounded Knapsack │ ├── Unbounded Knapsack.cpp │ └── a.exe ├── Wines-Problem │ ├── input.txt │ ├── output.txt │ └── wines_problem.cpp ├── activity_selection.cpp ├── boolean-parenthesization-problem.cpp ├── catalan_number │ ├── a.out │ └── nth_catalan_number.cpp ├── coin change problem │ ├── coinchange.cpp │ ├── coinchange1.cpp │ └── coinchange2.cpp ├── convert_array_A_to_B │ ├── convert_a_to_b.cpp │ └── problem.txt ├── distinct-palindromic-substring │ └── code.cpp ├── edit distance.cpp ├── edit distance.py ├── matrix_chain_mul │ └── c++ │ │ └── matrix_chain_mul.cpp ├── primitive_calculator.cpp └── word break.cpp ├── Graph ├── BFS graph.c ├── BFS_shortest_path.cpp ├── Bellman-Ford Algorithm │ ├── Bellman-Ford.cpp │ └── bellmanford.py ├── Bellmann_Ford.java ├── DFS graph.c ├── Dijkstras Algorithm │ └── Python │ │ ├── Dijkstras Algo.py │ │ └── dijkstra_algo.cpp ├── Flood_Fill_Algorithm.cpp ├── Floyd Warshall Algorithm │ └── Floyd Warshall Algorithm.py ├── Graph_Valid_Tree.py ├── Is Graph Cyclic.java ├── KosarajuAlgorithm.cpp ├── Kruskal's Algorithm │ ├── Krushals Min Cost Spanning Tree.cpp │ ├── kruskal_algo.py │ └── kruskal_algorithm.cpp ├── Number_of_islands.cpp ├── Prim's Minimum Spanning Trees │ ├── Prim's MST.py │ └── prims.cpp ├── Strongly Connected Graphs │ └── Strongly Connected Graphs.py ├── TopologicalSort.cpp ├── bfsTraversal.cpp ├── floodfill.c ├── maxArea.py └── rotting_oranges.cpp ├── Hashing ├── ColorfulNumber.cpp ├── DoubleHashingC++.cpp ├── Hash Table Class Chaining.cpp ├── Hash Table Quadratic Probing.cpp ├── HashTableLinearProbingC++.cpp ├── Largest_continuous_Sequence_zero_sum.cpp └── anagrams.cpp ├── Is Graph Cyclic.java ├── Leetcode problems ├── 1342-no-of-steps.cpp ├── 148. Sort List.cpp ├── 1539-kth-missing-ele.cpp ├── 162-find-peak-ele.cpp ├── 2342. Max Sum of a Pair With Equal Sum of Digits.cpp ├── 237-Delete Node in a Linked List.cpp ├── 237-Delete Node in a Linked List.java ├── 2416-Sum of Prefix Scores of Strings.java ├── 279-Perfect Squares.cpp ├── 3SumClosest.cpp ├── 4. Median of Two Sorted Arrays.java ├── 403.FrogJump.cpp ├── 416.PartitionEqualSubsetSum.cpp ├── 42. Trapping rain water ├── 480 Sliding Window Median.cpp ├── 53. Maximum Subarray.cpp ├── 540-single-element.cpp ├── 583.DeleteOperationForTwoStrings.cpp ├── 58Length-of-Last-Word.py ├── 69.RotateList.cpp ├── 70.Climbing_Stairs.cpp ├── 740-find-smallest-letter.cpp ├── 740. Delete and Earn.cpp ├── 838-push-dominoes.java ├── 88. Merge Sorted Array.cpp ├── 9-palindrome-number.cpp ├── 98-ValidateBinarySearchTree.cpp ├── Break a Palindrome.cpp ├── Cheapest_flights_within_k_stops.cpp ├── Cherry_PickupII.cpp ├── ContainerWithMostWater.cpp ├── Convert BST to Greater Tree.java ├── Decode Ways.cpp ├── Detect loop in linked list_ayush91985.cpp ├── Find_First_and_Last_Position_of_Element_in_Sorted_Array.cpp ├── GenerateParanthesis.cpp ├── H-index.py ├── HouseRobberII.cpp ├── InsertInterval.java ├── IntegerBreak.cpp ├── LRU_Cache.cpp ├── LongestCharacters.cpp ├── LongestConsecutiveSequence.cpp ├── LongestIncreasingSubseq.java ├── LongestSubString.cpp ├── Longest_Valid_Parentheses.cpp ├── Lowest Common Ancestor of a BST.cpp ├── Max_area_histogram.py ├── My _Calendar_III.cpp ├── NumberOfConnectedIslands.cpp ├── NumberOfProvinces.cpp ├── PartitionToKEqualSumSubsets.cpp ├── Path Sum.cpp ├── Permutation Sequence.cpp ├── Permutations II.cpp ├── Permutations.cpp ├── Populating Next Right Pointers in Each Node.cpp ├── Pow(x,n)problem.cpp ├── Rat_in_a_maze.cpp ├── RemoveDuplicates.cpp ├── ReturnToOrigin.java ├── Reverse_nodes_in_K_group.cpp ├── Set_matrix_zeros.cpp ├── ShoppingOffers.cpp ├── Shortest Unsorted Continuous Subarray.txt ├── SpiralMatrix.cpp ├── Split Linked List in Parts.cpp ├── SubrectangleQueries.cpp ├── Subset.java ├── Substring_with_Concatenation_of_All_Words.cpp ├── Sudoku_Solver.cpp ├── Swap Nodes in Pairs.cpp ├── ThirdMaxNumber.java ├── UglyNumberII.cpp ├── UniquePaths.cpp ├── Validate Binary Search Tree.cpp ├── WiggleSubsequence.cpp ├── a.out ├── arrangeCoins.py ├── atoi.py ├── bst.py ├── combination_sum.cpp ├── compare_version.cpp ├── concatenationOfArray.java ├── countCompleteTreeNodes.java ├── count_and_say.cpp ├── count_good_nodes_BT.cpp ├── diameter_of_binary_tree.py ├── factorial_trailing_zeroes.cpp ├── firstmissingpositive.cpp ├── flipping_an_image.java ├── hotel_review.cpp ├── house_robber.cpp ├── is_bipartite_leetcode_785.cpp ├── jump_game2.cpp ├── kdiff-pairs-in-array.cpp ├── longest_common_prefix.cpp ├── longest_common_prefix.py ├── longest_palindromic_sebsequence.cpp ├── max-product-subarray.cpp ├── maximum product subarray.cpp ├── median_of_two_sorted_arrays.java ├── merge_sorted_arrays.py ├── merge_sorted_list.cpp ├── minimum_path_sum.py ├── missing_no_of_AP.cpp ├── n_ary_tree_preorder.cpp ├── no_of_island_leetcode_200.cpp ├── pairSumOptimised.cpp ├── pallindrome.cpp ├── permutation.py ├── plus_one.cpp ├── prisoncellsndays.py ├── removeNthNodefromEnd.cpp ├── remove_nth_node.py ├── reverse_integer.cpp ├── reversebinarytreelevel.py ├── roman_to_integr.cpp ├── rotate2darray.cpp ├── rotateArray.cpp ├── search-in-2d-matrix.cpp ├── search_in2darray-II.cpp ├── set-matrix-zeroes-leetcode.cpp ├── single_number.py ├── slidingwinmax.cpp ├── sortColors.cpp ├── stick_prob.cpp ├── stock buy and sell.cpp ├── string permutation.java ├── suduko_solver.cpp ├── test.cpp ├── trie_implementation.py ├── two_sum.cpp ├── valid-anagram.js ├── valid_parenthesis.cpp └── word_break.cpp ├── Linked_List ├── Circular_linkedlist.cpp ├── Delete every N nodes.cpp ├── DeleteWithoutHeadPtr.cpp ├── K-Reverse in LinkedList.java ├── LinkedListReversePartial.cpp ├── Linked_List_Cycle.cpp ├── MiddleNodeLinkedlist.cpp ├── Reverse a Linked List in groups ├── Vectors │ ├── Printing.cpp │ └── VectorsusingPairs.cpp ├── a.out ├── addTwoNumbers.cpp ├── add_anywhere_in_linklist.c ├── add_nodes.cpp ├── adding_node.cpp ├── check_pallindrome.cpp ├── del_llist.cpp ├── dele_by_pos.cpp ├── delete in CLL.c ├── deleting_node.cpp ├── detect cycle in Linkedlist.cpp ├── get_length.cpp ├── get_nth_node.cpp ├── insert_Circular linked list.c ├── insert_delete_at_any_position_reverse_List.cpp ├── javalinkedlist │ ├── Solution.class │ ├── closestPrime.class │ └── closestPrime.java ├── lnkd_lst_trvsl.cpp ├── merge_two_sorted_linked_lists.cpp ├── music_playlist.c ├── nth_node_end.cpp ├── nth_node_end.exe ├── remove_duplicates.cpp ├── reverse_list.cpp ├── reverse_list_in_k_groups.cpp ├── search_key.cpp ├── single_linked_list.cpp └── subtract_linked_list.cpp ├── ModularExponentiation.cpp ├── Queue ├── circularQueue.cpp ├── implement_queue_using_stack.cpp ├── queueUsingArray.cpp ├── queueUsingLinkedList.cpp ├── reverse_a_queue.cpp ├── reverse_queue_using_recursion.cpp └── sort_queue.cpp ├── README.md ├── Recursion ├── Factorial_using_recursion.cpp ├── Fibonacci.java ├── Flood_fill_algorithm.cpp ├── LetterCasePermutation.cpp ├── N-Queens_problem.java ├── N-Queens_recursion.java ├── N_Queen.cpp ├── Print Encodings.cpp ├── Rat in a Maze 1.cpp ├── Target Sum Subsets.cpp ├── Tower_of_Hanoi.cpp ├── Tree_Traversal.cpp ├── factorial_using_recursion.py ├── number of dice roll with target sum.cpp ├── palindromic_partioning.cpp └── stack_sort.cpp ├── Searching algos ├── Binary-Search.py ├── BinarySearch.java ├── Binary_Search.cpp ├── Exponential_Search.cpp ├── Fibonacci_Search.c ├── Find_Transition_Point.cpp ├── Interpolation_Search.cpp ├── Interpolation_Search.py ├── Jump_Search.cpp ├── Linear_Search.cpp ├── Linear_Search.java ├── Linear_Search.py ├── Order_Agnostic_Binary_Search.java └── binarySearch.java ├── Set operations ├── cartesian.c ├── differenceset.c ├── intersection.c ├── powerset.c ├── symmetricdiff.c └── union.c ├── Sort ├── Bubble_Sort.py ├── BucketSort.cpp ├── Count_Sort.py ├── CountingSort.cpp ├── CycleSort.cpp ├── DNF_sort.cpp ├── Heap_Sort.java ├── Heap_Sort.py ├── Insertion sort.cpp ├── InsertionSort.py ├── MergeSort.c ├── QuickSort.c ├── QuickSort.py ├── QuickSort_poojaagrawal134.cpp ├── QuickSort_poojaagrawal134.exe ├── RadixSort.cpp ├── Selection sort.cpp ├── SelectionSort.py ├── TopologicalSort.cpp ├── TopologicalSort.py ├── bitonicsort.py ├── brick_sort.cpp ├── bubble sort.cpp ├── bubble_sort.java ├── cocktail_sort.cpp ├── cocktail_sort.py ├── comb_sort.cpp ├── dnf-sort.cpp ├── heap sort.cpp ├── insertion.c ├── merge sort.cpp ├── mergesort.cpp ├── mergesort.py ├── multiple_sort_menu_driven.cpp ├── pigeonholeSort.cpp ├── qsort.c ├── quicksort.cpp ├── shellSort.py ├── shellsort.c ├── tim_sort.cpp └── wavesort.cpp ├── Stack ├── DecimalToBinaryStack.java ├── Stack.c ├── Stack.py ├── delete_middle.cpp ├── infixTopostfix-conversion.cpp ├── jobSchedule.cpp ├── postfix_evaluation.c ├── python │ └── balanced_parenthesis.py ├── stack_implementation_using_queue.cpp └── stl_stack.cpp ├── TicTacToe.cpp ├── Top Interview Questions ├── Interview_Questions │ ├── Water_Trapped.cpp │ ├── best_time_to_buy_sell_stock.cpp │ ├── delete_middle.cpp │ ├── fizz_buzz.cpp │ ├── operator_overloading.cpp │ └── rainwater_trapping.cpp ├── Link to Excel File.txt └── Solutions │ ├── Arrays │ ├── CyclicallyRotateAnArrayByOne.py │ ├── FindTheMaximumAndMinimumElementInAnArray.py │ ├── Kadane'sAlgorithm.py │ ├── KthSmallElement.py │ ├── MoveAllNegativeNumbersToBeginning.py │ ├── ReverseTheArray.py │ ├── SortAnArrayOf0s,1sAnd2s.py │ └── UnionOfTwoArrays.py │ └── Matrix │ ├── FindMedianInARowWiseSortedMatrix.py │ ├── FindRowWithMaximumNo.Of1's.ipynb.py │ ├── SearchAnElementInAMatriix.py │ └── SpiralTraversalOnAMatrix.py ├── Trees ├── AVL_TREE.cpp ├── BinaryTree.c ├── Boundary_Traversal_of_Binary_Tree.cpp ├── Level Order Traversal.cpp ├── RBTrees.cpp ├── a.out ├── diameter-of-binaryTree.java ├── identicalTree.cpp ├── inorder_traversal.c ├── minimum depth of binary tree.cpp ├── postorder_traversal.c ├── preorder_traversal.c ├── reverse_level_order.cpp ├── spiral_traversal_tree.cpp ├── trees.c └── zigzag_order_traversal_binary_tree.cpp ├── array ├── ArrayTraversal.c └── Euclid's Algo ├── bitwiseOperators ├── Find_ith_bit.java ├── OddEven.java ├── SwapNumbers.java ├── binarys1sCompliment.java └── swap_ith_and_jth_bit_position.py ├── bubblesort.java ├── contests ├── codechef │ ├── DSA-Learning_C1 │ │ ├── a.out │ │ ├── life_uiver_evry.py │ │ └── reverse_integer.cpp │ ├── oct20long │ │ ├── Add square.cpp │ │ ├── D-Dimensional MST │ │ ├── a.out │ │ ├── chef_and_easy_queries.cpp │ │ ├── corona_run.cpp │ │ ├── digit_removal.cpp │ │ ├── longest_bitwise_subarray.cpp │ │ ├── mexor.cpp │ │ ├── mixture.cpp │ │ ├── positive_and.cpp │ │ ├── replace_x.cpp │ │ └── three_boxes.cpp │ ├── sep19long │ │ ├── a.out │ │ └── factorial.cpp │ ├── sep20_lunch │ │ ├── a.out │ │ ├── gcd_operation.cpp │ │ └── watermelon.cpp │ ├── sep20long │ │ ├── a.out │ │ ├── ada_matrix.cpp │ │ ├── chefina_swap.cpp │ │ ├── corona.cpp │ │ └── stick_prob.cpp │ └── subtask.cpp └── hackerearth │ ├── a.out │ └── tstwpen.cpp ├── cpp_template.cpp ├── dfs ├── Dfs.Cpp └── inorder.java └── trash_codes ├── Cpp ├── classes.cpp ├── compiler │ ├── a.out │ ├── bracket.h │ ├── multiply.cpp │ └── ot.txt ├── constructor.cpp ├── count_num_of_heap_var.cpp ├── destructor.cpp ├── enums.cpp ├── inheritence.cpp ├── linking │ ├── log.cpp │ └── multi.cpp ├── log_app_classes.cpp ├── pointer.cpp ├── references.cpp ├── static │ ├── a.out │ ├── local_static.cpp │ ├── main.cpp │ ├── staic_in_class_or_struct.cpp │ └── static.cpp ├── trikiest_oops_interview_question.cpp └── virtual_function.cpp ├── Math ├── binary_represntation.cpp ├── celcius_to_farenheit.py ├── decimal_to_binary.cpp ├── find_square_root.cpp ├── gcd.cpp ├── lcm.cpp ├── nth_fibonacci.cpp ├── pallindrome.cpp ├── prime.cpp └── sieveof_Eratosthenes.cpp ├── MockVita_2020 ├── Q3TCS_ CodeVita - Coding Arena.pdf ├── Q4TCS_ CodeVita - Coding Arena.pdf ├── Q5TCS_ CodeVita - Coding Arena.pdf ├── Q6TCS_ CodeVita - Coding Arena.pdf ├── TCS_ CodeVita - Coding Arena.pdf ├── TCS_ CodeVita - Coding Arenaq2.pdf ├── a.out ├── chocklate_dist.cpp └── swayamvar.cpp ├── Threading_in_C └── Simple_thread.c ├── host.txt ├── k_lucky_num.cpp ├── kth_element.cpp ├── remove_element.py └── tes.cpp /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "vector": "cpp", 4 | "c_inverse_of_permutation": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /Algorithms/CocktailSort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Algorithms/CocktailSort -------------------------------------------------------------------------------- /Algorithms/Graph/.cph/.Kadane's_algorithm.cpp_1d25845e7d2ec1b81fac4c8baeb39b9b.prob: -------------------------------------------------------------------------------- 1 | {"name":"Local: Kadane's_algorithm","url":"/Users/aishwarya/Desktop/Hacktober2022/kadane/dsalgo/Algorithms/Graph/Kadane's_algorithm.cpp","tests":[{"id":1665209636376,"input":"1\n8\n-2 -3 4 -1 -2 1 5 -3","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/Users/aishwarya/Desktop/Hacktober2022/kadane/dsalgo/Algorithms/Graph/Kadane's_algorithm.cpp","group":"local","local":true} -------------------------------------------------------------------------------- /Algorithms/Graph/Kadane's_algorithm.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Algorithms/Graph/Kadane's_algorithm.bin -------------------------------------------------------------------------------- /Algorithms/LCS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int max(int a, int b); 5 | int lcs( char *X, char *Y, int m, int n ) 6 | { 7 | if (m == 0 || n == 0) 8 | return 0; 9 | if (X[m-1] == Y[n-1]) 10 | return 1 + lcs(X, Y, m-1, n-1); 11 | else 12 | return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n)); 13 | } 14 | 15 | int max(int a, int b) 16 | { 17 | return (a > b)? a : b; 18 | } 19 | 20 | int main() 21 | { 22 | char X[] = "AGGTAB"; 23 | char Y[] = "GXTXAYB"; 24 | 25 | int m = strlen(X); 26 | int n = strlen(Y); 27 | 28 | cout<<"Length of Longest Common Subsequence is "<< lcs( X, Y, m, n ) ; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Algorithms/Math/BinaryExponentiation.cpp: -------------------------------------------------------------------------------- 1 | /* This algorithm allows us to calculate a^n in O(lg(n)) time. */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | long long binaryExponentiation(int a, int n) 7 | { 8 | if (!n) // Base case 9 | return 1; 10 | long long result = binaryExponentiation(a, n / 2); 11 | if (n % 2) 12 | return result * result * a; 13 | else 14 | return result * result; 15 | } 16 | 17 | int main() 18 | { 19 | int a, n; 20 | cin >> a >> n; 21 | if (n < 0) 22 | cout << "Enter positive exponent." << endl; 23 | else if (a == 0 && n == 0) 24 | cout << "Not defined." << endl; 25 | else 26 | { 27 | long long result = binaryExponentiation(a, n); 28 | cout << result << endl; 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Algorithms/Math/Binomial Coefficient.cpp: -------------------------------------------------------------------------------- 1 | // A recursive C++ implementation 2 | #include 3 | using namespace std; 4 | 5 | // Returns value of Binomial Coefficient C(n, k) 6 | int binomialCoeff(int n, int k) 7 | { 8 | // Base Cases 9 | if (k == 0 || k == n) 10 | return 1; 11 | 12 | // Recur 13 | return binomialCoeff(n - 1, k - 1) + 14 | binomialCoeff(n - 1, k); 15 | } 16 | 17 | /* Driver code*/ 18 | int main() 19 | { 20 | int n = 5, k = 2; 21 | cout << "Value of C("< 11 | using namespace std; 12 | 13 | // Finding the n_th catalan number 14 | unsigned long findCatalan(unsigned n) 15 | { 16 | if (n <= 1) 17 | return 1; 18 | unsigned long result = 0; 19 | for (int i = 0; i < n; i++) 20 | result = result + findCatalan(i) * findCatalan(n - i - 1); 21 | return result; 22 | } 23 | 24 | int main() 25 | { 26 | unsigned n; 27 | cin >> n; 28 | for (unsigned i = 0; i < n; i++) 29 | cout << findCatalan(i) << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Algorithms/Math/FermatsLittleTheorem.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | // to fing (a^b)%m 6 | int power(int a, unsigned b, unsigned m) 7 | { 8 | if (!b) 9 | return 1; 10 | int res = power(a, b / 2, m) % m; 11 | res = (res * res) % m; 12 | if (b % 2 == 0) 13 | return res; 14 | return (a * res) % m; 15 | } 16 | 17 | // to find gcd 18 | int gcd(int a, int b) 19 | { 20 | if (!b) 21 | return a; 22 | return gcd(b, a % b); 23 | } 24 | 25 | void modularInverse(int a, int m) 26 | { 27 | int gcd_res = gcd(a, m); 28 | if (gcd_res != 1) 29 | cout << "The numbers are not co-prime" << endl; 30 | else 31 | { 32 | // modular Inverse = (a^(m-2))%m; 33 | cout << "Mod inverse: " << power(a, m - 2, m) << endl; 34 | } 35 | return; 36 | } 37 | 38 | int main() 39 | { 40 | int a, m; 41 | cin >> a >> m; 42 | modularInverse(a, m); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Algorithms/Math/Matrix Exponentiation/Matrix Exponentiation.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Algorithms/Math/Matrix Exponentiation/bin/Debug/Matrix Exponentiation.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Algorithms/Math/Matrix Exponentiation/bin/Debug/Matrix Exponentiation.exe -------------------------------------------------------------------------------- /Algorithms/Math/Matrix Exponentiation/obj/Debug/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Algorithms/Math/Matrix Exponentiation/obj/Debug/main.o -------------------------------------------------------------------------------- /Algorithms/Math/NewtonsMethod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | double newtonsMethod(std::function f, std::function df, double initialGuess, double tol=1e-5, int max_iter=100) { 6 | double x = initialGuess; 7 | double old_x = initialGuess; 8 | for(int i = 0; i < max_iter; i++) { 9 | x -= f(old_x) / df(old_x); 10 | if(std::abs(x - old_x) < tol) { 11 | return x; 12 | } 13 | old_x = x; 14 | } 15 | return x; 16 | } 17 | 18 | double nthRoot(double x, int n) { 19 | return newtonsMethod([=](double t){return std::pow(t, n) - x;}, [=](double t){return n * std::pow(t, n - 1);}, 2 * x); 20 | } 21 | 22 | int main() { 23 | double x, n; 24 | std::cin >> x >> n; 25 | std::cout << nthRoot(x, n) << std::endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Algorithms/Math/golden_ratio.cpp: -------------------------------------------------------------------------------- 1 | // CPP program to find n-th Fibonacci number 2 | #include 3 | using namespace std; 4 | 5 | // Approximate value of golden ratio 6 | double PHI = 1.6180339; 7 | 8 | // Fibonacci numbers upto n = 5 9 | int f[6] = { 0, 1, 1, 2, 3, 5 }; 10 | 11 | // Function to find nth 12 | // Fibonacci number 13 | int fib (int n) 14 | { 15 | // Fibonacci numbers for n < 6 16 | if (n < 6) 17 | return f[n]; 18 | 19 | // Else start counting from 20 | // 5th term 21 | int t = 5, fn = 5; 22 | 23 | while (t < n) { 24 | fn = round(fn * PHI); 25 | t++; 26 | } 27 | 28 | return fn; 29 | } 30 | 31 | // driver code 32 | int main() 33 | { 34 | int n = 9; 35 | 36 | cout << n << "th Fibonacci Number = " 37 | << fib(n) << endl; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Algorithms/MatrixChainMultiplication.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int MatrixChainOrder(int p[], int i, int j) 5 | { 6 | if (i == j) 7 | return 0; 8 | int k; 9 | int min = INT_MAX; 10 | int count; 11 | for (k = i; k < j; k++) 12 | { 13 | count = MatrixChainOrder(p, i, k) 14 | + MatrixChainOrder(p, k + 1, j) 15 | + p[i - 1] * p[k] * p[j]; 16 | 17 | if (count < min) 18 | min = count; 19 | } 20 | 21 | return min; 22 | } 23 | 24 | int main() 25 | { 26 | int arr[] = { 1, 2, 3, 4, 3 }; 27 | int n = sizeof(arr) / sizeof(arr[0]); 28 | 29 | cout << "Minimum number of multiplications is " 30 | << MatrixChainOrder(arr, 1, n - 1); 31 | } 32 | -------------------------------------------------------------------------------- /Algorithms/Recursion/Coin_Change_Recursion/c++/CoinChange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int coinchange(int amt, int c[],int n,int currentcoin) 4 | { 5 | if(amt==0) 6 | return 1; 7 | 8 | if(amt<0) 9 | return 0; 10 | int ways=0; 11 | for(int i=currentcoin;i>n; 22 | int c[n]; 23 | //enter the coin denimation in array of size n 24 | for(int i=0;i>c[i]; 27 | } 28 | //enter the amount for which you need no. of ways to get that amount 29 | int amt; 30 | cin>>amt; 31 | cout<<"no. of ways to get amount from given denominations is :"< 2 | using namespace std; 3 | 4 | int josephus(int n, int k) 5 | { 6 | if (n == 1) 7 | return 1; 8 | else 9 | /* The position returned by josephus(n - 1, k) 10 | is adjusted because the recursive call 11 | josephus(n - 1, k) considers the 12 | original position k % n + 1 as position 1 */ 13 | return (josephus(n - 1, k) + k - 1) % n + 1; 14 | } 15 | 16 | // Driver Program to test above function 17 | int main() 18 | { 19 | int n = 14; 20 | int k = 2; 21 | cout << "The chosen place is " << josephus(n, k); 22 | return 0; 23 | } 24 | 25 | // This code is contributed by vikash kumar patel 26 | -------------------------------------------------------------------------------- /Algorithms/RodCutting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int max(int a, int b) { return (a > b)? a : b;} 7 | int cutRod(int price[], int n) 8 | { 9 | int val[n+1]; 10 | val[0] = 0; 11 | int i, j; 12 | 13 | for (i = 1; i<=n; i++) 14 | { 15 | int max_val = INT_MIN; 16 | for (j = 0; j < i; j++) 17 | max_val = max(max_val, price[j] + val[i-j-1]); 18 | val[i] = max_val; 19 | } 20 | 21 | return val[n]; 22 | } 23 | 24 | int main() 25 | { 26 | int arr[] = {1, 5, 8, 9, 10, 17, 17, 20}; 27 | int size = sizeof(arr)/sizeof(arr[0]); 28 | cout <<"Maximum Obtainable Value is "< 4 | using namespace std; 5 | 6 | int binarySearch(int array[], int x, int low, int high) { 7 | 8 | // Repeat until the pointers low and high meet each other 9 | while (low <= high) { 10 | int mid = low + (high - low) / 2; 11 | 12 | if (array[mid] == x) 13 | return mid; 14 | 15 | if (array[mid] < x) 16 | low = mid + 1; 17 | 18 | else 19 | high = mid - 1; 20 | } 21 | 22 | return -1; 23 | } 24 | 25 | int main(void) { 26 | int array[] = {3, 4, 5, 6, 7, 8, 9}; 27 | int x = 4; 28 | int n = sizeof(array) / sizeof(array[0]); 29 | int result = binarySearch(array, x, 0, n - 1); 30 | if (result == -1) 31 | printf("Not found"); 32 | else 33 | printf("Element is found at index %d", result); 34 | } 35 | -------------------------------------------------------------------------------- /Algorithms/gcdEucidAlgo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd (int a , int b ) { 5 | if(a ==0 ) { 6 | return a; 7 | } 8 | if(b==0) { 9 | return b; 10 | } 11 | 12 | while( a != b) { 13 | if(a > b) { 14 | a=a-b; 15 | } 16 | else{ 17 | b=b-a; 18 | } 19 | 20 | } 21 | return a; 22 | } 23 | 24 | int main(){ 25 | int a ; 26 | int b; 27 | cout<<"Enter no a: "<>a; 29 | cout<<"Enter no b: "<>b; 31 | 32 | int ans = gcd(a , b); 33 | cout<<"The gcd of " < 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | int N; 7 | int i=0; 8 | cout<<"Enter the number of elements in the array"<>N; 10 | int arr[N]; 11 | cout<<"Enter elements of array"<>arr[i]; 14 | } 15 | int current_sum=0; 16 | int start,end,s=0; 17 | int max_sum=0; 18 | for(i=0;imax_sum){ 21 | max_sum=current_sum; 22 | start=s; 23 | end=i; 24 | } 25 | if(current_sum<0){ 26 | current_sum=0; 27 | s=i+1; 28 | } 29 | //max_sum=max(current_sum,max_sum); 30 | } 31 | cout<<"Maximum sum in subarray is "< 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int size,count=1,id=0; 8 | cout<<"enter the size of an array"<>size; 10 | int arr[size]; 11 | cout<<"enter the number of element"<>arr[i]; 15 | } 16 | for(int i =1;i 2 | using namespace std; 3 | #define int long long 4 | 5 | signed main() { 6 | int n; 7 | cout<<"Enter size of array-"<> n; 9 | 10 | vector a(n); 11 | cout<<"Enter array of "<> a[i]; 14 | } 15 | 16 | priority_queue, greater> minheap; 17 | for(int i=0; i 1) { 24 | int e1 = minheap.top(); 25 | minheap.pop(); 26 | int e2 = minheap.top(); 27 | minheap.pop(); 28 | 29 | ans += e1 + e2; 30 | minheap.push(e1 + e2); 31 | } 32 | cout<<"Minimum Value- "<& prices) 3 | { 4 | if(prices.size() == 0) 5 | return 0; 6 | 7 | int Soln = 0; 8 | int begin = prices[0], finish = prices[0]; 9 | 10 | for(int i = 0; i < prices.size(); i++) 11 | { 12 | if(prices[i] < begin){ 13 | //start compared to 0 14 | Soln = max(Soln, finish - begin); 15 | begin = prices[i]; 16 | finish = prices[i]; 17 | } 18 | else 19 | { 20 | //continue 21 | finish = max(finish, prices[i]); 22 | } 23 | } 24 | Soln = max(Soln, finish - begin); 25 | return Soln; 26 | } 27 | -------------------------------------------------------------------------------- /Arrays/Check_Repeated_digits_using_arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int r,n,seen[10]={0}; 6 | cout<<"Enter a number: "; 7 | cin>>n; 8 | while(n!=0) 9 | { 10 | r=n%10; 11 | if(seen[r]==1) 12 | break; 13 | seen[r]=1; 14 | n/=10; 15 | } 16 | if(n>0) 17 | cout<<"Yes there are repeated digits in the entered number."; 18 | else 19 | cout<<"No there are no repeated digits in this number."; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Arrays/Find the missing number in a consecutive array list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Our own function taking an array and its size as parameters 5 | int getMissingNumber(int a[], int n){ 6 | int i, total; 7 | 8 | total= ((n+1)*(n+2))/2; 9 | for(i=0; i 2 | using namespace std; 3 | 4 | 5 | 6 | void longest_subarray(int *arr, int n, int diff){ 7 | sort(arr, arr+n); 8 | 9 | 10 | 11 | 12 | } 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | // Driver code 22 | int main(){ 23 | int n, diff; 24 | cin>>n; 25 | int arr[n]; 26 | for (int i=0; i>arr[i] 28 | } 29 | cout >> "enter the differenc between two elements of the subarray: "; 30 | cin>> diff; 31 | longest_subarray(arr, n, diff); 32 | } 33 | -------------------------------------------------------------------------------- /Arrays/SecondLargestInArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class SecondLargestInArray{ 3 | public static int getSecondLargest(Integer[] a, int total){ 4 | List list=Arrays.asList(a); 5 | Collections.sort(list); 6 | int element=list.get(total-2); 7 | return element; 8 | } 9 | public static void main(String args[]){ 10 | Integer a[]={1,2,5,6,3,2}; 11 | Integer b[]={44,66,99,77,33,22,55}; 12 | System.out.println("Second Largest: "+getSecondLargest(a,6)); 13 | System.out.println("Second Largest: "+getSecondLargest(b,7)); 14 | }} 15 | -------------------------------------------------------------------------------- /Arrays/Sum_of_numbers_in_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin>>n; 8 | int a[n]; 9 | for (int i = 0; i < n; i++) 10 | { 11 | cin>>a[i]; 12 | } 13 | int sum=0; 14 | for (int j = 0; j< n; j++) 15 | { 16 | sum = sum+a[j]; 17 | } 18 | 19 | cout< val = new HashMap<>(); 8 | for(int i=0;i 4 | using namespace std; 5 | 6 | int binarySearch(int array[], int x, int low, int high) { 7 | 8 | // Repeat until the pointers low and high meet each other 9 | while (low <= high) { 10 | int mid = low + (high - low) / 2; 11 | 12 | if (array[mid] == x) 13 | return mid; 14 | 15 | if (array[mid] < x) 16 | low = mid + 1; 17 | 18 | else 19 | high = mid - 1; 20 | } 21 | 22 | return -1; 23 | } 24 | 25 | int main(void) { 26 | int array[] = {3, 4, 5, 6, 7, 8, 9}; 27 | int x = 4; 28 | int n = sizeof(array) / sizeof(array[0]); 29 | int result = binarySearch(array, x, 0, n - 1); 30 | if (result == -1) 31 | printf("Not found"); 32 | else 33 | printf("Element is found at index %d", result); 34 | } 35 | -------------------------------------------------------------------------------- /Arrays/catalan_numbers.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to find the nth Catalan Number 2 | #include 3 | using namespace std; 4 | 5 | // Returns value of Binomial Coefficient C(n, k) 6 | unsigned long int binomialCoeff(unsigned int n, 7 | unsigned int k) 8 | { 9 | unsigned long int res = 1; 10 | 11 | if (k > n - k) 12 | k = n - k; 13 | 14 | for (int i = 0; i < k; ++i) { 15 | res *= (n - i); 16 | res /= (i + 1); 17 | } 18 | 19 | return res; 20 | } 21 | 22 | // A Binomial coefficient based function to find nth catalan 23 | // number in O(n) time 24 | unsigned long int catalan(unsigned int n) 25 | { 26 | unsigned long int c = binomialCoeff(2 * n, n); 27 | 28 | return c / (n + 1); 29 | } 30 | 31 | // Driver code 32 | int main() 33 | { 34 | for (int i = 0; i < 10; i++) 35 | cout << catalan(i) << " "; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Arrays/conversion_number_System.cpp: -------------------------------------------------------------------------------- 1 | //debug this 2 | //binary to decimal 3 | //octal to decimal 4 | //hexadecimal to decimal 5 | //reverse of all 6 | #include 7 | #include 8 | using namespace std; 9 | int max_power_division(int m,int n){ 10 | // m is no. n is divide 11 | int i=0; 12 | int power=n; 13 | while(m>power){ 14 | power=pow(n,i); 15 | i++; 16 | } 17 | i--; 18 | return m/power; 19 | } 20 | int conversion(int from,int to,int value){ 21 | int final =0; 22 | while(value>=10){ 23 | final=final*10 + (value/max_power_division(value,to)); 24 | value=value/10; 25 | } 26 | return final; 27 | } 28 | int main(){ 29 | int hexa = conversion(10,8,58); 30 | cout< 2 | using namespace std; 3 | int fact(int); 4 | int main() 5 | { 6 | int n,f; 7 | 8 | cin>>n; 9 | f=fact(n); 10 | cout< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | template 8 | 9 | void printArray(vector v){ 10 | for(int i=0; i> n; 19 | vector int_vector(n); 20 | for (int i = 0; i < n; i++) { 21 | int value; 22 | cin >> value; 23 | int_vector[i] = value; 24 | } 25 | 26 | cin >> n; 27 | vector string_vector(n); 28 | for (int i = 0; i < n; i++) { 29 | string value; 30 | cin >> value; 31 | string_vector[i] = value; 32 | } 33 | 34 | printArray(int_vector); 35 | printArray(string_vector); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Arrays/kadane.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Kadane's Algorithm 5 | 6 | int kadane(int arr[], int n){ 7 | int ans = arr[0]; 8 | int count = arr[0]; 9 | for(int i = 0; i < n; i++){ 10 | count = max(arr[i], count+arr[i]); 11 | ans = max(ans, count); 12 | } 13 | return ans; 14 | } 15 | 16 | int main() { 17 | int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; 18 | int n = sizeof(a)/sizeof(a[0]); 19 | 20 | cout << kadane(a, n) << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Arrays/longest_range_ofelement_array.py: -------------------------------------------------------------------------------- 1 | 2 | def longest_range(array): 3 | lar_range = [] 4 | max_lenth = 0 5 | map_num = {} 6 | for num in array: 7 | map_num[num] = True 8 | for num in array: 9 | if not map_num[num]: 10 | continue 11 | else: 12 | map_num[num] = False 13 | current_count = 1 14 | left = num-1 15 | right = num+1 16 | while left in map_num: 17 | map_num[left] = False 18 | current_count+=1 19 | left -= 1 20 | while right in map_num: 21 | map_num[right] = False 22 | current_count += 1 23 | right += 1 24 | if current_count >max_lenth: 25 | max_lenth = current_count 26 | lar_range = [left+1, right-1] 27 | 28 | print(max_lenth, lar_range) 29 | 30 | longest_range([1,11,3,0, 15, 5, 2, 4, 10, 7, 12, 6]) 31 | -------------------------------------------------------------------------------- /Arrays/magical_chessboard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | jay has a magical chessboard . it looks like a normal except its size is N . Therefore the number of cells in the chessboard is N*N . He is intrested in counting the number of squares in the chessborad . it is a very tough task for him , so he is looking for your help 3 | sample input 4 | 3 5 | 1 6 | 2 7 | 3 8 | output 9 | 1 10 | 5 11 | 14 12 | 13 | constraints 14 | 1<= T <= 100 15 | 1<= N <= 100 16 | */ 17 | 18 | #include 19 | using namespace std ; 20 | int main (){ 21 | int T ,j ; 22 | cin >> T; 23 | int arr[100]; 24 | for (int i=0 ; i> j ; 27 | arr[i]= j; 28 | } 29 | for (int k=0 ; k0 ;i--) 33 | { 34 | ans = (ans + (i*i)); 35 | } 36 | cout << ans << endl; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Arrays/max_absolute_diff.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int maxArr(vector &A){ 4 | int n=A.size(),max1=INT_MIN,max2=INT_MIN,min1=INT_MAX,min2=INT_MAX,ans=INT_MIN; 5 | for(int i=0;i>n; 18 | vector v; 19 | for(auto i=0; i>input; 21 | v.push_back(input); 22 | } 23 | cout<{ 3 | if(!A.length) 4 | return 0; 5 | let ans = 0; 6 | let sum = 0; 7 | for(const element of A){ 8 | sum += element; 9 | ans = Math.max(sum,ans); 10 | if(sum<0){ 11 | sum=0; 12 | } 13 | } 14 | return ans; 15 | } 16 | 17 | const array = [1,2,3,4,-1,5]; 18 | console.log(getMaxSum(array)); -------------------------------------------------------------------------------- /Arrays/max_sum_subarray.cpp: -------------------------------------------------------------------------------- 1 | // @dheeraj 2 | // Max sum of subarra using Kaden's Algorithms 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | long int max_sum_subarr(vector& arr) { 10 | long int max_curr=arr[0]; 11 | long int max_global=arr[0]; 12 | 13 | for(int i=1; i max_global) max_global=max_curr; 16 | } 17 | return max_global; 18 | } 19 | 20 | int main(){ 21 | int n, input; 22 | cin>>n; 23 | vector v; 24 | for(auto i=0; i>input; 26 | v.push_back(input); 27 | } 28 | cout<a[i]) 30 | {min=a[i];} 31 | } 32 | System.out.println("Smallest Element in array :"+min); 33 | System.out.println("Largest Element in array :"+max); 34 | 35 | }} 36 | -------------------------------------------------------------------------------- /Arrays/moore_voting_algo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int mod = 1e9+7; 4 | 5 | int findMajority(vectorvec, int n) 6 | { 7 | int i, candidate = -1, votes = 0; 8 | 9 | for (i = 0; i < n; i++) { 10 | if (votes == 0) { 11 | candidate = vec[i]; 12 | votes = 1; 13 | } 14 | else { 15 | if (vec[i] == candidate) 16 | votes++; 17 | else 18 | votes--; 19 | } 20 | } 21 | int count = 0; 22 | for (i = 0; i < n; i++) { 23 | if (vec[i] == candidate) 24 | count++; 25 | } 26 | 27 | if (count > n / 2) 28 | return candidate; 29 | return -1; 30 | } 31 | int main() 32 | { 33 | vectorvec{1, 1, 1, 1, 2, 3, 4}; 34 | int vec_size = vec.size(); 35 | 36 | cout << "The majority element is : " << findMajority(vec, vec_size); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Arrays/negative_nos_at_one_end.cpp: -------------------------------------------------------------------------------- 1 | //C++ program to put all the negative numbers before 2 | //positive numbers in an array. The order of 3 | //elements is not important here 4 | 5 | #include 6 | using namespace std; 7 | 8 | //function to rearrange the array- negative no's 9 | //at first, followed by positive no's. 10 | //QuickSort partition process is applied here 11 | void rearrange(int arr[],int n){ 12 | int j=0; 13 | for(int i=0;i& nums) { 6 | int idx = -1; 7 | int n = nums.size(); 8 | for(int i=n-1; i>0; i--){ 9 | if(nums[i]>nums[i-1]){ 10 | idx = i; 11 | break; 12 | } 13 | } 14 | if(idx == -1){ 15 | reverse(nums.begin(),nums.end()); 16 | }else{ 17 | int prev = idx; 18 | for(int i=idx+1; inums[idx-1] and nums[i]<=nums[prev]){ 20 | prev = i; 21 | } 22 | } 23 | swap(nums[idx-1],nums[prev]); 24 | reverse(nums.begin()+idx, nums.end()); 25 | 26 | } 27 | } 28 | 29 | }; 30 | -------------------------------------------------------------------------------- /Arrays/nth_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | // // O(2^n) Time | O(n) Space 7 | // int nth_fibo(int num){ 8 | // if(num == 1) 9 | // return 0; 10 | // if(num == 2) 11 | // return 1; 12 | // 13 | // else{ 14 | // int fib = nth_fibo(num-1) + nth_fibo(num-2) ; 15 | // return fib; 16 | // } 17 | // } 18 | 19 | // O(n) Time | O(1) Space 20 | int nth_fibo(int num){ 21 | vector v{0, 1}; 22 | int count=3; 23 | while(count<= num){ 24 | int fib = v[0] + v[1]; 25 | v[0] = v[1]; 26 | v[1] = fib; 27 | count++; 28 | } 29 | return num >1 ? v[1] : v[0]; 30 | } 31 | 32 | int main(){ 33 | int n; 34 | cin>>n; 35 | cout< 4 | vector> pairSum(vector &arr, int s){ 5 | vector> ans; 6 | 7 | for(int i=0; i pair; 11 | pair.push_back(min(arr[i],arr[j])); 12 | pair.push_back(max(arr[i],arr[j])); 13 | ans.push_back(pair); 14 | } 15 | } 16 | 17 | } 18 | sort(ans.begin(),ans.end()); 19 | return ans; 20 | } 21 | -------------------------------------------------------------------------------- /Arrays/pascal_triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | vector > solve(int A) { 7 | vector< vector > result; 8 | for( int i =0 ; i (i+1,1)); 10 | for(int j=1; j>n; 22 | // vector v(n); 23 | // for(auto i=0; i>input; 25 | // v.push_back(input); 26 | // } 27 | solve(n); 28 | // for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int n; 6 | cin>>n; 7 | int c=2; 8 | for(int i=2;i<=n;i++){ 9 | if(i% n/2 == 0){// we sort search by half n or sqrt(n func in header math header 10 | cout<<"\n Number is not prime"; 11 | //break; 12 | return 1; //completely get out of main 13 | //what if we pass return; 14 | } 15 | c=i; 16 | } 17 | cout<<"Hello"; 18 | if(c==n){ 19 | cout<<"\nNumber is prime"; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Arrays/priority_queue_make_your_own.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int main(int argc, char const *argv[]) { 7 | 8 | auto mycomparator = [](int left, int right){return (left) < (right) ; }; // this comparator is used to give priority to the element with your rule 9 | priority_queue, decltype(mycomparator)> dheeraj_pq(mycomparator); 10 | 11 | for(int ele: {1,2,4,21,6,8}){ 12 | dheeraj_pq.push(ele); 13 | } 14 | 15 | while(!dheeraj_pq.empty()){ 16 | cout< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | 6 | int N; 7 | cin >> N; 8 | regex e(".+@gmail\\.com$"); 9 | vector db; 10 | 11 | for(int a0 = 0; a0 < N; a0++){ 12 | string firstName; 13 | string emailID; 14 | cin >> firstName >> emailID; 15 | if(regex_match(emailID,e)){ 16 | db.push_back(firstName); 17 | } 18 | } 19 | 20 | sort(db.begin(),db.end()); 21 | for(int i = 0;i < db.size();i++) { 22 | cout << db[i] << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Arrays/removeDuplicates.py: -------------------------------------------------------------------------------- 1 | arr = [1,2,3,2,4,6,3,7,3,9,7,0,6,8,5,4] #random array 2 | new_arr = [] #temporary array 3 | 4 | for elem in arr: 5 | if elem not in new_arr: 6 | new_arr.append(elem) 7 | 8 | arr = new_arr 9 | -------------------------------------------------------------------------------- /Arrays/spiral_matrix_traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int solve(vector< vector > &B, int n, int m) { 6 | 7 | int left, 8 | 9 | 10 | // int x = B.size(); 11 | // sort(B.begin(), B.end()); 12 | cout<>n; 26 | cin>>m; 27 | vector< vector > v(n, vector (m)); 28 | for(int i=0; i>input; 31 | v[i].push_back(input); 32 | } 33 | } 34 | solve(v, n, m); 35 | // for(int i=0; i 2 | using namespace std; 3 | int main(){ 4 | int n; 5 | cin>>n; 6 | vector v; 7 | for(int i =0; i>input; 10 | v.push_back(input); 11 | } 12 | cout << v.size()< 2 | #include 3 | using namespace std; 4 | 5 | vector solve(vector &B) { 6 | // int x = B.size(); 7 | sort(B.begin(), B.end()); 8 | for(int i=0; i>n; 18 | vector v(n); 19 | for(auto i=0; i>input; 21 | v.push_back(input); 22 | } 23 | solve(v); 24 | for(int i=0; i > &ans){ 2 | if(root==NULL) 3 | return; 4 | queue q; 5 | int level=1; 6 | q.push(root); 7 | while(!q.empty()){ 8 | int count=q.size(); 9 | vector v; 10 | for(int i=0;ival); 14 | if(curr->left!=NULL) 15 | q.push(curr->left); 16 | if(curr->right!=NULL) 17 | q.push(curr->right); 18 | 19 | } 20 | if(level%2==0) 21 | reverse(v.begin(),v.end()); 22 | level++; 23 | ans.push_back(v); 24 | } 25 | } 26 | 27 | vector > Solution::zigzagLevelOrder(TreeNode* A) { 28 | vector > ans; 29 | TreeNode *root=A; 30 | levelorder(root,ans); 31 | return ans; 32 | } 33 | -------------------------------------------------------------------------------- /Binary_Search_Tree/bstInNodeInRange.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | int ans = 0; 11 | // utility function to traverse the BST 12 | void fun(TreeNode* root, int b, int c){ 13 | if(!root) return; 14 | // incresing the ans variable if current value is in the range [b,c] 15 | if(root->val >= b && root->val <= c) ans++; 16 | // traversing both the subtrees. 17 | fun(root->right, b, c); 18 | fun(root->left, b,c); 19 | 20 | } 21 | int solve(TreeNode* A, int B, int C) { 22 | // initializing the ans variable to 0 before traversal. 23 | ans = 0; 24 | fun(A,B,C); 25 | return ans; 26 | } 27 | -------------------------------------------------------------------------------- /Binary_tree/Build-heightbalanced-BinaryTree/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Binary_tree/Build-heightbalanced-BinaryTree/.DS_Store -------------------------------------------------------------------------------- /Binary_tree/Build-heightbalanced-BinaryTree/inputf.in: -------------------------------------------------------------------------------- 1 | 7 2 | 1 2 3 4 5 6 7 -------------------------------------------------------------------------------- /Binary_tree/Build-heightbalanced-BinaryTree/outputf.in: -------------------------------------------------------------------------------- 1 | 4 2 | 2 6 3 | 1 3 5 7 4 | -------------------------------------------------------------------------------- /Binary_tree/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Binary_tree/a.out -------------------------------------------------------------------------------- /CODECHEF EASY/ALTER.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin >> t; 9 | while (t--) 10 | { 11 | long long int a, b, p, q; 12 | cin >> a >> b >> p >> q; 13 | int x, y; 14 | if (p % a != 0 || q % b != 0) 15 | { 16 | cout << "NO" << endl; 17 | } 18 | else 19 | { 20 | x = p / a; 21 | y = q / b; 22 | if (abs(y - x) == 1|| y - x == 0) 23 | { 24 | cout << "YES" << endl; 25 | } 26 | else 27 | { 28 | cout << "NO" << endl; 29 | } 30 | } 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /CODECHEF EASY/CHEFCAR.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long int 4 | #define all(x) (x).begin(), (x).end() 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | int t; 10 | cin >> t; 11 | while (t--) 12 | { 13 | 14 | ll n, v; 15 | cin >> n >> v; 16 | if (n == 1) 17 | { 18 | cout << "0 0" << endl; 19 | continue; 20 | } 21 | ll mx = (n * (n - 1)) / 2; 22 | // ll mx=((n-2)*(n-1))/2+(n-1)*v; 23 | ll mn = v; 24 | mn += ((n - v) * (n - v + 1)) / 2; 25 | mn--; 26 | if (v >= n) 27 | { 28 | cout << mx << " " << n - 1 << endl; 29 | } 30 | else 31 | { 32 | cout << mx << " " << mn << endl; 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /CODECHEF EASY/CHEFSUM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | int min, T; 7 | cin>>T; 8 | while(T-->0){ 9 | 10 | int N,index; 11 | index=0; 12 | min=100001 ; 13 | cin>>N; 14 | int a[N]; 15 | 16 | for (int i=0; i>a[i]; 18 | if (a[i]0) : 4 | n = int(input()) 5 | fact = 1 6 | 7 | for i in range(1,n+1): 8 | fact= fact*i 9 | 10 | print(fact) 11 | 12 | t=t-1 -------------------------------------------------------------------------------- /CODECHEF EASY/FLOW018.cpp: -------------------------------------------------------------------------------- 1 | /*---------koolgax99---------------*/ 2 | #include 3 | using namespace std; 4 | #define gc getchar_unlocked 5 | #define fo(i, n) for (i = 1; i <= n; i++) 6 | #define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1) 7 | #define ll long long 8 | 9 | int main() 10 | { 11 | ios_base::sync_with_stdio(0); 12 | ll int t, i, j, k, p, q = 1, r, x, y, u, v, n, m; 13 | cin >> t; 14 | while (t--) 15 | { 16 | ll int q = 1; 17 | cin >> p; 18 | 19 | fo(i, p) 20 | { 21 | q = q * i; 22 | } 23 | 24 | cout << q << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /CODECHEF EASY/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | // b has less ASCII value than g 6 | char leftStr[] = "ggf"; 7 | char rightStr[] = "gfg"; 8 | 9 | int res = strcmp(leftStr, rightStr); 10 | 11 | if (res == 0) 12 | printf("Strings are equal"); 13 | else 14 | printf("Strings are unequal"); 15 | 16 | printf("\nValue returned by strcmp() is: %d", res); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /CODECHEF EASY/test.py: -------------------------------------------------------------------------------- 1 | # cook your dish here 2 | while True: 3 | n = int(input()) 4 | if n == 0: 5 | break 6 | else: 7 | count = 1 8 | a = list(map(int, input().split())) 9 | for i in range(n): 10 | if (a[int(a[i])-1] != i+1): 11 | 12 | count = 0 13 | print("not ambiguous") 14 | break 15 | if count != 0: 16 | print("ambiguous") 17 | 18 | 19 | -------------------------------------------------------------------------------- /CSES problems/Course schedule/problem.md: -------------------------------------------------------------------------------- 1 | https://cses.fi/problemset/task/1679 -------------------------------------------------------------------------------- /CodeForces Problems/230_A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ios::sync_with_stdio(false); 5 | cin.tie(0); 6 | cout.tie(0); 7 | int s,n,row=0,flag=0; 8 | cin>>s>>n; 9 | vector> v; 10 | for(int i=0; i drogon(2); 12 | for(auto &it:drogon){ 13 | cin>>it; 14 | } 15 | v.push_back(drogon); 16 | } 17 | sort(v.begin(), v.end()); 18 | while(s>v[row][0]){ 19 | flag++; 20 | s+=v[row][1]; 21 | if(row==n-1){ 22 | break; 23 | } 24 | else{ 25 | row++; 26 | } 27 | } 28 | if(row==n-1 && flag==n){ 29 | cout<<"YES\n"; 30 | } 31 | else{ 32 | cout<<"NO\n"; 33 | } 34 | return 0; 35 | } -------------------------------------------------------------------------------- /CodeForces Problems/330_A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n,m,countr=0,countc=0; 5 | cin>>n>>m; 6 | vector v; 7 | for(int i=0; i>s; 11 | v.push_back(s); 12 | for(int j=0; j 2 | using namespace std; 3 | int main(){ 4 | int n,k,count=0; 5 | cin>>n>>k; 6 | vector v(n); 7 | for(auto &it:v){ 8 | cin>>it; 9 | } 10 | sort(v.begin(),v.end()); 11 | if(n<3){ 12 | cout<<0<=k){ 17 | count++; 18 | } 19 | } 20 | cout< 2 | using namespace std; 3 | int main(){ 4 | string s,s1; 5 | std::getline(std::cin, s); 6 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,m,a,b,cost1,cost2; 6 | cin>>n>>m>>a>>b; 7 | if(b>(a*m)){ 8 | cout< 2 | using namespace std; 3 | string a, b = "hello"; 4 | int j = 0, pas = 0; 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(0); 8 | cout.tie(0); 9 | cin >> a; 10 | for (int i = 0; i < a.size(); i++) { 11 | if (a[i] == b[j]) { 12 | j++; 13 | if (j == 5) { 14 | break; 15 | } 16 | } 17 | } 18 | if (j == 5) { 19 | cout << "YES"; 20 | } else { 21 | cout << "NO"; 22 | } 23 | } -------------------------------------------------------------------------------- /CodeForces Problems/CF_146_A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | string s; 8 | long long int str[26]={0},count=0; 9 | cin>>s; 10 | long long int l=s.length(); 11 | for(long long int i=0;i 4 | using namespace std; 5 | 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | while(t--) { 12 | 13 | //Input 14 | string s; 15 | cin >> s; 16 | 17 | int n = s.size(); 18 | 19 | //No matter which action is chosen, after this action is performed exactly one letter 'B' is erased from the string and exactly two letters in total are erased from the string 20 | 21 | int cntB = 0; 22 | int cntAC = 0; 23 | 24 | for (int i = 0; i < n; i++) { 25 | if (s[i] == 'B') cntB++; 26 | else cntAC++; 27 | } 28 | 29 | if (cntAC == cntB) cout << "YES" << endl; 30 | else cout << "NO" << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /CodeForces Problems/CF_750_A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define int long long 4 | void solve(){ 5 | int a,b,c; 6 | cin>>a>>b>>c; 7 | int sum=a+2*b+3*c; 8 | if(sum%2){ 9 | cout<<"1"; 10 | } 11 | else{ 12 | cout<<"0"; 13 | } 14 | return; 15 | } 16 | int32_t main(){ 17 | int t=1;cin>>t; 18 | while(t--){ 19 | solve(); 20 | cout<<"\n"; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /CodeForces Problems/CF_791_A.c: -------------------------------------------------------------------------------- 1 | // Problem link : https://codeforces.com/contest/791/problem/A 2 | 3 | #include 4 | int main(){ 5 | 6 | int a,b,c=0; 7 | 8 | scanf("%d %d",&a,&b); 9 | 10 | //Loop to add age 11 | while (a <= b){ 12 | a = 3*a; 13 | b = 2*b; 14 | 15 | c=c+1; 16 | } 17 | 18 | printf("%d",c); 19 | } 20 | -------------------------------------------------------------------------------- /CodeForces Problems/C_Inverse_Of_Permutation: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(0); 9 | cout.tie(0); 10 | int n; 11 | cin>>n; 12 | unordered_mapm; 13 | for(int i=0;i>a; 16 | m.insert({a,i+1}); 17 | } 18 | for(int i=1;i<=n;i++){ 19 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int len; 8 | string s; 9 | cin>>len; 10 | cin>>s; 11 | vector vect; 12 | 13 | while(len!=0){ 14 | if(len%2==1){ 15 | vect.push_back(s[0]); 16 | }else{ 17 | vect.insert(vect.begin(),s[0]); 18 | } 19 | s.erase(0,1); 20 | len=s.length(); 21 | } 22 | for(int i=0;i //Default file present on CodeStudio Site don't change or manipulate it 2 | void sort012(int *arr, int n) 3 | { 4 | for(int i=0;i> 1) + (max_val & 1) 13 | replace_max(heap, new_val) 14 | 15 | return sum(heap) 16 | n, k = map(int,input().split()) 17 | number = list(map(int,input().split())) 18 | 19 | s = minSum(number,k) 20 | print(s) 21 | -------------------------------------------------------------------------------- /CodeVita/paste_reduction.py: -------------------------------------------------------------------------------- 1 | string_one=input() 2 | string_two=input() 3 | var1={} 4 | var2=[] 5 | j=0 6 | while(j 3 | using namespace std; 4 | 5 | // Returns value of Binomial Coefficient C(n, k) 6 | int binomialCoeff(int n, int k) 7 | { 8 | // Base Cases 9 | if (k > n) 10 | return 0; 11 | if (k == 0 || k == n) 12 | return 1; 13 | 14 | // Recur 15 | return binomialCoeff(n - 1, k - 1) 16 | + binomialCoeff(n - 1, k); 17 | } 18 | 19 | /* Driver code*/ 20 | int main() 21 | { 22 | int n = 5, k = 2; 23 | cout << "Value of C(" << n << ", " << k << ") is " 24 | << binomialCoeff(n, k); 25 | return 0; 26 | } 27 | 28 | // This is code is contributed by rathbhupendra 29 | -------------------------------------------------------------------------------- /DP/Binomialcoefficient/binomial2.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for space optimized Dynamic Programming 2 | // Solution of Binomial Coefficient 3 | #include 4 | using namespace std; 5 | 6 | int binomialCoeff(int n, int k) 7 | { 8 | int C[k + 1]; 9 | memset(C, 0, sizeof(C)); 10 | 11 | C[0] = 1; // nC0 is 1 12 | 13 | for (int i = 1; i <= n; i++) 14 | { 15 | 16 | // Compute next row of pascal triangle using 17 | // the previous row 18 | for (int j = min(i, k); j > 0; j--) 19 | C[j] = C[j] + C[j - 1]; 20 | } 21 | return C[k]; 22 | } 23 | 24 | /* Driver code*/ 25 | int main() 26 | { 27 | int n = 5, k = 2; 28 | cout << "Value of C(" << n << "," << k << ")"<< "is " < 6 | using namespace std; 7 | 8 | int getnth_fibonaacci(int n) { 9 | double phi = (1 + sqrt(5)) / 2; 10 | return round(pow(phi, n) / sqrt(5)); 11 | } 12 | 13 | // Driver Code 14 | int main() { 15 | /* code */ 16 | long int n; 17 | cout<<"Enter the nth term: "; 18 | cin>>n; 19 | cout< 6 | using namespace std; 7 | 8 | int getnth_fibonaacci(int n){ 9 | // int n1=0, n2=1; 10 | long long int fibo[n+2]; 11 | long int i; 12 | fibo[0]=0; // Declraing 0th term and 1th term to be 1. 13 | fibo[1]=1; 14 | for (i=2; i<=n; i++){ 15 | fibo[i] = fibo[i-1] + fibo[i-2]; //Adding previous two terms and storing it 16 | } 17 | return fibo[n]; 18 | } 19 | 20 | // Driver Code 21 | int main() { 22 | /* code */ 23 | long int n; 24 | cout<<"Enter the nth term: "; 25 | cin>>n; 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main(){ 6 | string s1="XMJYAUZ"; 7 | string s2="MZJAWXU"; 8 | //cin>>s1; 9 | // cin>>s2; 10 | int i,j; 11 | int m=s1.length(); 12 | int n=s2.length(); 13 | int lookup[m+1][n+1]; 14 | for(i=0;i<=m;i++){ 15 | lookup[i][0]=0; 16 | } 17 | 18 | for(i=0;i<=n;i++){ 19 | lookup[0][i]=0; 20 | } 21 | 22 | for(i=1;i<=m;i++){ 23 | for(j=1;j<=n;j++){ 24 | if(s1[i-1]==s2[j-1]) 25 | lookup[i][j]=lookup[i-1][j-1]+1; 26 | else 27 | lookup[i][j]=max(lookup[i-1][j],lookup[i][j-1]); 28 | } 29 | } 30 | cout<<"largest common subsequence "< int: 5 | ''' 6 | Kadane's algorithm to find maximum subarray sum 7 | Time complexity: O(n) 8 | ''' 9 | best_sum = -infinity 10 | sum_ = 0 11 | for item in array: 12 | sum_ = max(item, sum_ + item) 13 | best_sum = max(sum_, best_sum) 14 | 15 | return best_sum 16 | 17 | if __name__ == '__main__': 18 | test = (13, -3, -25, 20, -16, -20, -25, 8, 20, -2, 11, -24, -47, 35) 19 | print(max_subarray_sum(test)) 20 | -------------------------------------------------------------------------------- /DP/Needleman-Wunsch/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/DP/Needleman-Wunsch/a.exe -------------------------------------------------------------------------------- /DP/Nussinov Algorithm/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/DP/Nussinov Algorithm/a.exe -------------------------------------------------------------------------------- /DP/Permutation Coefficient/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/DP/Permutation Coefficient/a.out -------------------------------------------------------------------------------- /DP/Recursive-Function/Recursive function.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 3 | Design and implement a non-recursive dynamic programming based algorithm to 4 | evaluate f(n) = 2(f(n − 1) ∗ f(n − 2))) + 1, where n ≥ 2, f(0) = 1 and f(1) = 2. 5 | 6 | ******************************************************************************/ 7 | #include 8 | using namespace std; 9 | 10 | long int sum(long int n) 11 | { 12 | long int a[n+2]; 13 | a[0]=1; 14 | a[1]=2; 15 | a[2]=5; 16 | for (long int i=3;i<=n;i++) 17 | a[i]=2*a[i-1]*a[i-2] + a[i-1]; 18 | return (a[n]); 19 | } 20 | int main() 21 | { 22 | long int n; 23 | char c='y'; 24 | while(c == 'y' || c == 'Y') 25 | { 26 | cout<<"\nEnter a number: "<>n; 28 | cout<<"Answer : "<>c; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DP/Rod Cutting Problem/C++/RodCutDP: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/DP/Rod Cutting Problem/C++/RodCutDP -------------------------------------------------------------------------------- /DP/Segmented Least Squares/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/DP/Segmented Least Squares/a.exe -------------------------------------------------------------------------------- /DP/Shortest Common Supersequence/problemStatement.txt: -------------------------------------------------------------------------------- 1 | Given two strings X and Y, find the length of the shortest sequence that includes both X and Y as its subsequence. 2 | 3 | For eg: 4 | 5 | Input: X= "AGGTAB" Y="GXTXAYB" 6 | Output: 9 7 | Explaination: The shortest such sequence is "AGXGTXAYB". 8 | 9 | Constraints: 1<=|X|,|Y|<=100 -------------------------------------------------------------------------------- /DP/Sticker theif problem/problem statement: -------------------------------------------------------------------------------- 1 | There are n houses build in a line, each of which contains some value in it. A thief is going to steal the maximal value of these houses, but he can’t steal in two adjacent houses because the owner of the stolen houses will tell his two neighbors left and right side. What is the maximum stolen value? 2 | Examples: 3 | 4 | 5 | Input: houseVal[] = {6, 7, 1, 3, 8, 2, 4} 6 | Output: 19 7 | 8 | Explanation: The thief will steal 6, 1, 8 and 4 from the house. 9 | 10 | Input: houseVal[] = {5, 3, 4, 11, 2} 11 | Output: 16 12 | 13 | Explanation: Thief will steal 5 and 11 -------------------------------------------------------------------------------- /DP/Subset-sum/DP table for example.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | N=4 4 | A[]={3, 4, 5, 2} 5 | K=6 6 | 7 | 0 1 2 3 4 5 6 8 | 9 | 0 T F F F F F F 10 | 11 | 3 T F F T F F F 12 | 13 | 4 T F F T T F F 14 | 15 | 5 T F F T T T F 16 | 17 | 2 T F T T T T T -------------------------------------------------------------------------------- /DP/Ugly_Number/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/DP/Ugly_Number/a.out -------------------------------------------------------------------------------- /DP/Unbounded Knapsack/a.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/DP/Unbounded Knapsack/a.exe -------------------------------------------------------------------------------- /DP/Wines-Problem/input.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 2 4 6 2 5 -------------------------------------------------------------------------------- /DP/Wines-Problem/output.txt: -------------------------------------------------------------------------------- 1 | 64 2 | -------------------------------------------------------------------------------- /DP/activity_selection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int activitySelection(vector start, vector end, int n){ 5 | priority_queue, vector>, greater>> pq; 6 | for(int i=0; iprev){ 14 | prev=pq.top().first; 15 | count++; 16 | } 17 | 18 | pq.pop(); 19 | } 20 | 21 | return count; 22 | } 23 | 24 | int main() 25 | { 26 | int n; 27 | cin>>n; 28 | 29 | vector start(n), end(n); 30 | for(int i=0; i>start[i]; 32 | cin>>end[i]; 33 | } 34 | 35 | cout< 7 | using namespace std; 8 | 9 | unsigned long int get_nth_catalan_number(unsigned int n){ 10 | unsigned long int cat[n+1]; 11 | cat[0]=1; 12 | cat[1]=1; 13 | for (int i=2; i<=n; i++){ 14 | cat[i]=0; 15 | for (int j=0; j>n; 28 | cout< 2 | using namespace std; 3 | 4 | int coinchange(vector& a, int v, int n, 5 | vector >& dp) 6 | { 7 | if (v == 0) 8 | return dp[n][v] = 1; 9 | if (n == 0) 10 | return 0; 11 | if (dp[n][v] != -1) 12 | return dp[n][v]; 13 | if (a[n - 1] <= v) { 14 | // Either Pick this coin or not 15 | return dp[n][v] = coinchange(a, v - a[n - 1], n, dp) 16 | + coinchange(a, v, n - 1, dp); 17 | } 18 | else // We have no option but to leave this coin 19 | return dp[n][v] = coinchange(a, v, n - 1, dp); 20 | } 21 | int32_t main() 22 | { 23 | int tc = 1; 24 | // cin >> tc; 25 | while (tc--) { 26 | int n, v; 27 | n = 3, v = 4; 28 | vector a = { 1, 2, 3 }; 29 | vector > dp(n + 1, 30 | vector(v + 1, -1)); 31 | int res = coinchange(a, v, n, dp); 32 | cout << res << endl; 33 | } 34 | } 35 | // This Code is Contributed by 36 | // Harshit Agrawal NITT 37 | -------------------------------------------------------------------------------- /DP/edit distance.py: -------------------------------------------------------------------------------- 1 | # Function to find Levenshtein Distance between X and Y 2 | # m and n are the number of characters in X and Y respectively 3 | def dist(X, m, Y, n): 4 | 5 | # base case: empty strings (case 1) 6 | if m == 0: 7 | return n 8 | 9 | if n == 0: 10 | return m 11 | 12 | # if last characters of the strings match (case 2) 13 | cost = 0 if (X[m - 1] == Y[n - 1]) else 1 14 | 15 | return min(dist(X, m - 1, Y, n) + 1, # deletion (case 3a)) 16 | dist(X, m, Y, n - 1) + 1, # insertion (case 3b)) 17 | dist(X, m - 1, Y, n - 1) + cost) # substitution (case 2 + 3c) 18 | 19 | 20 | if __name__ == '__main__': 21 | 22 | X = "kitten" 23 | Y = "sitting" 24 | 25 | print("The Levenshtein Distance is", dist(X, len(X), Y, len(Y))) 26 | -------------------------------------------------------------------------------- /Graph/Graph_Valid_Tree.py: -------------------------------------------------------------------------------- 1 | def validTree(self, n, edges): 2 | if not n: 3 | return True 4 | adj = {i: [] for i in range(n)} 5 | for n1, n2 in edges: 6 | adj[n1].append(n2) 7 | adj[n2].append(n1) 8 | 9 | visit = set() 10 | 11 | def dfs(i, prev): 12 | if i in visit: 13 | return False 14 | 15 | visit.add(i) 16 | for j in adj[i]: 17 | if j == prev: 18 | continue 19 | if not dfs(j, i): 20 | return False 21 | return True 22 | 23 | return dfs(0, -1) and n == len(visit) 24 | -------------------------------------------------------------------------------- /Graph/TopologicalSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int n,m; 5 | cin>>n>>m; 6 | int cnt = 0; 7 | vector> adj(n); 8 | vector indegre(n,0); 9 | for(int i=0;i>u>>v; 12 | adj[u].push_back(v); 13 | indegre[v]++; 14 | } 15 | queue q; 16 | for(int i=0;i int: 2 | ROWS, COLS = len(grid), len(grid[0]) 3 | visit = set() 4 | 5 | def dfs(r, c): 6 | if ( 7 | r < 0 8 | or r == ROWS 9 | or c < 0 10 | or c == COLS 11 | or grid[r][c] == 0 12 | or (r, c) in visit 13 | ): 14 | return 0 15 | visit.add((r, c)) 16 | return 1 + dfs(r + 1, c) + dfs(r - 1, c) + dfs(r, c + 1) + dfs(r, c - 1) 17 | 18 | area = 0 19 | for r in range(ROWS): 20 | for c in range(COLS): 21 | area = max(area, dfs(r, c)) 22 | return area 23 | -------------------------------------------------------------------------------- /Leetcode problems/1342-no-of-steps.cpp: -------------------------------------------------------------------------------- 1 | //leetcode 2 | //1342 3 | //Number of Steps to Reduce a Number to Zero 4 | 5 | 6 | 7 | class Solution { 8 | public: 9 | 10 | int helper(int num, int count) 11 | { 12 | if(num==0) 13 | return count; 14 | if(num%2==0) 15 | return helper(num/2,count+1); 16 | 17 | return helper(num-1,count+1); 18 | } 19 | int numberOfSteps(int num) { 20 | /* int res=0; 21 | while(num) 22 | { 23 | if((num & 1)==0) 24 | num>>=1; 25 | 26 | else 27 | num-=1; 28 | 29 | 30 | ++res; 31 | 32 | } 33 | return res; 34 | */ 35 | 36 | return helper(num,0); 37 | } 38 | }; -------------------------------------------------------------------------------- /Leetcode problems/1539-kth-missing-ele.cpp: -------------------------------------------------------------------------------- 1 | //leetcode 2 | //1539 3 | //kth missing element 4 | 5 | class Solution { 6 | public: 7 | int findKthPositive(vector& arr, int k) { 8 | int s=0,e=arr.size()-1,m; 9 | while(s<=e) 10 | { 11 | m=s+(e-s)/2; 12 | if((arr[m]-(m+1))>=k) 13 | e=m-1; 14 | else 15 | s=m+1; } 16 | return s+k; 17 | 18 | } 19 | }; -------------------------------------------------------------------------------- /Leetcode problems/162-find-peak-ele.cpp: -------------------------------------------------------------------------------- 1 | //leetcode 2 | //162 3 | //Find peak element 4 | 5 | class Solution { 6 | public: 7 | int findPeakElement(vector& nums) { 8 | int start=0; 9 | int end=nums.size()-1; 10 | while(startnums[mid+1]) //search in left 14 | end=mid; 15 | else if(nums[mid]& nums) { 12 | vector sumOfNums; 13 | for(int i: nums){ 14 | sumOfNums.push_back(findSum(i)); 15 | } 16 | int maxi = -1; 17 | unordered_map um; 18 | for(int i=0; inext; 20 | node->val=node->next->val; 21 | node->next=node->next->next; 22 | delete temp; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode problems/237-Delete Node in a Linked List.java: -------------------------------------------------------------------------------- 1 | /* 2 | Link to problem: https://leetcode.com/problems/delete-node-in-a-linked-list/ 3 | Difficulty: Medium 4 | */ 5 | 6 | /** 7 | * Definition for singly-linked list. 8 | * public class ListNode { 9 | * int val; 10 | * ListNode next; 11 | * ListNode(int x) { val = x; } 12 | * } 13 | */ 14 | class Solution { 15 | public void deleteNode(ListNode node) { 16 | node.val=node.next.val; 17 | node.next=node.next.next; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Leetcode problems/279-Perfect Squares.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSquares(int n) { 4 | vectordp(n+1,INT_MAX); 5 | dp[0]=0,dp[1]=1; 6 | for(int i=2;i<=n;i++) 7 | { 8 | for(int j=1;j*j<=i;j++) 9 | { 10 | dp[i]=min(dp[i],1+dp[i-j*j]); 11 | } 12 | } 13 | for(int i=0;i<=n;i++) 14 | cout< 3 | using namespace std; 4 | class Solution { 5 | public: 6 | int threeSumClosest(vector& nums, int target) { 7 | int n=nums.size(); 8 | int mini=INT_MAX; 9 | int ans; 10 | sort(nums.begin(),nums.end()); 11 | for(int i=0;i &nums) { 6 | 7 | if (sum == 0) { 8 | return true; 9 | } 10 | if (ind < 0) { 11 | return false; 12 | } 13 | 14 | if (sum < 0) { 15 | return false; 16 | } 17 | 18 | if (dp[ind][sum] != -1) { 19 | return dp[ind][sum]; 20 | } 21 | 22 | return dp[ind][sum] = subsetSum(ind - 1, sum, nums) || 23 | subsetSum(ind - 1, sum - nums[ind], nums); 24 | } 25 | 26 | bool canPartition(vector &nums) { 27 | int sum = accumulate(nums.begin(), nums.end(), 0); 28 | if (sum & 1) { 29 | return false; 30 | } 31 | 32 | memset(dp, -1, sizeof(dp)); 33 | 34 | return subsetSum(nums.size() - 1, sum / 2, nums); 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /Leetcode problems/42. Trapping rain water: -------------------------------------------------------------------------------- 1 | #trap rain water solution 2 | #runtime 4ms 3 | #linear time complexity 4 | #linear space complexity 5 | #simple solution 6 | class Solution { 7 | public: 8 | int trap(vector& height) 9 | { 10 | int n=height.size(); 11 | 12 | if(n==0)return 0; 13 | 14 | int hl[n],hr[n]; 15 | 16 | hl[0]=height[0],hr[n-1]=height[n-1]; 17 | 18 | for(int i=1;i=0;i--)hr[i]=max(hr[i+1],height[i]); 21 | 22 | int hm[n],sum=0; 23 | 24 | for(int i=0;i& nums) { 4 | int currSum = nums[0]; 5 | int ans = nums[0]; 6 | 7 | for(int i=1; i int: 3 | return len((s.strip().split(" ")[-1])) 4 | -------------------------------------------------------------------------------- /Leetcode problems/70.Climbing_Stairs.cpp: -------------------------------------------------------------------------------- 1 | //Ques Link: https://leetcode.com/problems/climbing-stairs/ 2 | //Approach: DP 3 | 4 | class Solution { 5 | public: 6 | int climbStairs(int n) { 7 | if(n == 1 || n == 2){ 8 | return n; 9 | } 10 | int dp[n+1]; 11 | memset(dp,0,sizeof(int)); 12 | dp[1] = 1; 13 | dp[2] = 2; 14 | 15 | for(int i=3;i<=n;i++){ 16 | dp[i] = dp[i-1] + dp[i-2]; 17 | } 18 | 19 | return dp[n]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode problems/740-find-smallest-letter.cpp: -------------------------------------------------------------------------------- 1 | //leetcode 2 | //740 3 | //Find Smallest Letter Greater Than Target 4 | 5 | 6 | class Solution { 7 | public: 8 | char nextGreatestLetter(vector& letters, char target) { 9 | 10 | int start =0; 11 | int end=letters.size()-1; 12 | 13 | 14 | 15 | while(start<=end) 16 | { 17 | int mid= start+(end-start)/2; 18 | if(target< letters[mid]) 19 | end= mid-1; 20 | else 21 | start=mid+1; 22 | 23 | 24 | } 25 | return letters[start%letters.size()]; 26 | 27 | } 28 | }; -------------------------------------------------------------------------------- /Leetcode problems/740. Delete and Earn.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int deleteAndEarn(vector& nums) { 4 | int n = nums.size(); 5 | int m = *max_element(nums.begin(), nums.end()); // max element in nums array 6 | vector count(m+1), dp(m+1); 7 | 8 | for(int i = 0; i < n; i++){ 9 | count[nums[i]] += nums[i]; 10 | } 11 | dp[0] = count[0]; 12 | dp[1] = max(count[0], count[1]); 13 | for(int i = 2; i <= m; i++){ 14 | dp[i] = max(dp[i-1], count[i]+dp[i-2]); 15 | } 16 | return dp[m]; 17 | } 18 | }; 19 | // store count of all the element then apply house robber algo on count array 20 | // 1, 2, 3, 1, 1 21 | // [0, 3, 1, 1] count of element 22 | // [0, 3, 2, 3] // multiplying element with count, Then apply bank robber algo; 23 | -------------------------------------------------------------------------------- /Leetcode problems/9-palindrome-number.cpp: -------------------------------------------------------------------------------- 1 | //9. Palindrome Number 2 | //Given an integer x, return true if x is palindrome integer. 3 | //An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not. 4 | 5 | class Solution { 6 | public: 7 | bool isPalindrome(int x) { 8 | //copy x into string 9 | string original_str = std::to_string(x); 10 | //make a reverse string 11 | string reverse_str; 12 | //add chars from x string to reverse str in reverse order 13 | for(int i=original_str.length()-1; i>=0; i--) 14 | { 15 | reverse_str += original_str[i]; 16 | } 17 | //if strings are equal they are palindrome 18 | if(original_str == reverse_str) 19 | return true; 20 | return false; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode problems/98-ValidateBinarySearchTree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool isValidBST(TreeNode* root) { 15 | return helper(root,LONG_MAX,LONG_MIN); 16 | } 17 | bool helper(TreeNode* root,long int mx,long int mn) 18 | {if(root==NULL ) 19 | return true; 20 | if(root->val>=mx or root->val<=mn) 21 | return false; 22 | return helper(root->left,root->val,mn) and helper(root->right,mx,root->val); 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode problems/Break a Palindrome.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Link to problem: https://leetcode.com/problems/break-a-palindrome/ 3 | Difficulty: Medium 4 | */ 5 | 6 | class Solution { 7 | public: 8 | string breakPalindrome(string p) { 9 | int n=p.length(); 10 | if(n==1) 11 | return ""; 12 | 13 | for(int i=0; i& height) { 6 | 7 | int i=0; 8 | int max = 0; 9 | int area = 1; 10 | int j = height.size()-1; 11 | while(i max) 15 | max = area; 16 | if(height[i] searchRange(vector& nums, int target) { 4 | int idx1 = lower_bound(nums, target); 5 | int idx2 = lower_bound(nums, target+1)-1; 6 | if (idx1 < nums.size() && nums[idx1] == target) 7 | return {idx1, idx2}; 8 | else 9 | return {-1, -1}; 10 | } 11 | 12 | int lower_bound(vector& nums, int target) { 13 | int l = 0, r = nums.size()-1; 14 | while (l <= r) { 15 | int mid = (r-l)/2+l; 16 | if (nums[mid] < target) 17 | l = mid+1; 18 | else 19 | r = mid-1; 20 | } 21 | return l; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Leetcode problems/GenerateParanthesis.cpp: -------------------------------------------------------------------------------- 1 | //22. Generate Parentheses 2 | // Link-https://leetcode.com/problems/generate-parentheses// 3 | class Solution 4 | { 5 | public: 6 | void solve(vector &s, int o, int c, int n, string op) 7 | { 8 | if (c == 0 && o == 0) 9 | { 10 | s.push_back(op); 11 | return; 12 | } 13 | if (o != 0) 14 | { 15 | string op1 = op; 16 | op1.push_back('('); 17 | solve(s, o - 1, c, n, op1); 18 | } 19 | if (c > o) 20 | { 21 | string op2 = op; 22 | op2.push_back(')'); 23 | solve(s, o, c - 1, n, op2); 24 | return; 25 | } 26 | } 27 | vector generateParenthesis(int n) 28 | { 29 | vector s; 30 | string op = ""; 31 | int c = n; 32 | int o = n; 33 | solve(s, o, c, n, op); 34 | return s; 35 | } 36 | }; -------------------------------------------------------------------------------- /Leetcode problems/IntegerBreak.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerBreak(int n) { 4 | if(n==2) 5 | return 1; 6 | if(n==3) 7 | return 2; 8 | int i,j; 9 | int dp[n+1][n+1]; 10 | for(i=0;i<=n;i++) 11 | { 12 | dp[i][0]=0;dp[0][i]=1; 13 | // dp[i][1]=1;dp[1][i]=1; 14 | } 15 | for(i=1;i<=n;i++) 16 | { 17 | for(j=1;j<=n;j++) 18 | { 19 | if(j>i) 20 | dp[i][j]=dp[i][j-1]; 21 | else 22 | dp[i][j]=max(dp[i][j-1],dp[i-j][j]*j); 23 | } 24 | } 25 | return dp[n][n]; 26 | } 27 | }; -------------------------------------------------------------------------------- /Leetcode problems/LongestCharacters.cpp: -------------------------------------------------------------------------------- 1 | /* Given a string s, find the length of the longest substring without repeating characters. */ 2 | // Link : https://leetcode.com/problems/longest-substring-without-repeating-characters/ 3 | 4 | int lengthOfLongestSubstring(string s) { 5 | vectormpp(256,-1); 6 | int left=0,right=0; 7 | int n=s.size(); 8 | int len=0; 9 | while(right s; 11 | int c = 0; 12 | int maxx = 1; 13 | if (st == "") 14 | return 0; 15 | if (st == " ") 16 | return 1; 17 | for (int i = 0; i < st.length(); i++) 18 | { 19 | if (s.find(st[i]) == s.end()) 20 | { 21 | s.insert(st[i]); 22 | c++; 23 | maxx = max(maxx, c); 24 | } 25 | else 26 | { 27 | 28 | i = i - c; 29 | c = 0; 30 | s.clear(); 31 | } 32 | } 33 | return maxx; 34 | } 35 | }; -------------------------------------------------------------------------------- /Leetcode problems/Longest_Valid_Parentheses.cpp: -------------------------------------------------------------------------------- 1 | //32. Longest Valid Parentheses 2 | //https://leetcode.com/problems/longest-valid-parentheses/ 3 | 4 | class Solution { 5 | public: 6 | int longestValidParentheses(string s) { 7 | 8 | int n=s.size(),matchidx,maxlen=0; 9 | vectordp(n,0); 10 | 11 | for(int i=1;i=0 && s[matchidx]=='('){ 18 | // ()()()--->2+dp[index-1] 19 | dp[i]+=2+dp[i-1]; 20 | 21 | // adding before matches 22 | if(matchidx-1>=0) 23 | dp[i]+=dp[matchidx-1]; 24 | 25 | } 26 | } 27 | 28 | // length of the longest 29 | maxlen=max(maxlen,dp[i]); 30 | } 31 | 32 | return maxlen; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /Leetcode problems/Lowest Common Ancestor of a BST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 14 | 15 | if(!root) return NULL; 16 | 17 | int curr = root->val; 18 | 19 | if(curr > p->val && curr > q->val) { 20 | return lowestCommonAncestor(root->left, p, q); 21 | } 22 | else if(curr < p->val && curr < q->val) { 23 | return lowestCommonAncestor(root->right, p, q); 24 | } 25 | 26 | else return root; 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /Leetcode problems/Path Sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | bool helper(TreeNode* root, int targetSum) { 15 | if (!root) return false; 16 | if (!root->left && !root->right) { 17 | return targetSum - root->val == 0; 18 | } 19 | 20 | return helper(root->left, targetSum - root->val) || helper(root->right, targetSum - root->val); 21 | } 22 | bool hasPathSum(TreeNode* root, int targetSum) { 23 | return helper(root, targetSum); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Leetcode problems/Permutations II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permuteUnique(vector& nums) { 4 | vector> ans; 5 | sort(nums.begin(),nums.end()); 6 | ans.push_back(nums); 7 | while(next_permutation(nums.begin(),nums.end())){ 8 | ans.push_back(nums); 9 | } 10 | return ans; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Leetcode problems/Permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> ans; 4 | vector v; 5 | int n; 6 | 7 | void dfs(vector& path, vector& vis){ 8 | if(path.size()==n){ 9 | ans.push_back(path); 10 | return; 11 | } 12 | for(int i=0;i> permute(vector& nums) { 24 | ans.clear(); 25 | v=nums; 26 | n=nums.size(); 27 | vector vis(n,0); 28 | vector path; 29 | dfs(path, vis); 30 | return ans; 31 | } 32 | }; -------------------------------------------------------------------------------- /Leetcode problems/Pow(x,n)problem.cpp: -------------------------------------------------------------------------------- 1 | //Link to problem:https://leetcode.com/problems/powx-n/ 2 | class Solution { 3 | 4 | double pow(double x,int n){ 5 | if(n==0){ 6 | return 1; 7 | } 8 | double temp=pow(x,n/2); 9 | if(n%2==0){ 10 | return temp*temp; 11 | } 12 | else{ 13 | return x*temp*temp; 14 | } 15 | } 16 | 17 | public: 18 | double myPow(double x, int n) { 19 | if(n<0){ 20 | return 1/pow(x,n); 21 | }else{ 22 | return pow(x,n); 23 | } 24 | 25 | } 26 | }; -------------------------------------------------------------------------------- /Leetcode problems/RemoveDuplicates.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 2 | 3 | class Solution { 4 | public: 5 | int removeDuplicates(vector& nums) { 6 | int i=0,j=1; 7 | int n = nums.size(); 8 | if(n==0) 9 | return 0; 10 | for(j=1;j 4 | using namespace std; 5 | class Solution { 6 | public: 7 | 8 | ListNode* reverseKGroup(ListNode* A, int B) { 9 | if(!A) 10 | return NULL; 11 | ListNode* t = A; 12 | for(int i=0;inext; 16 | } 17 | ListNode* curr = A; 18 | ListNode* prev = NULL; 19 | ListNode* sec = NULL; 20 | int count=0; 21 | while(curr && countnext; 23 | curr->next = prev; 24 | prev = curr; 25 | curr = sec; 26 | count++; 27 | } 28 | if(sec) 29 | A->next = reverseKGroup(sec,B); 30 | return prev; 31 | } 32 | }; -------------------------------------------------------------------------------- /Leetcode problems/SubrectangleQueries.cpp: -------------------------------------------------------------------------------- 1 | /**** Problem link: https://leetcode.com/problems/subrectangle-queries/ *// 2 | 3 | class SubrectangleQueries { 4 | public: 5 | vector> vec; 6 | vector,int> > up; 7 | SubrectangleQueries(vector>& rectangle) { 8 | ios_base::sync_with_stdio(false); 9 | cin.tie(NULL); 10 | vec=rectangle; 11 | } 12 | 13 | void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) { 14 | up.push_back({{row1,col1,row2,col2},newValue}); 15 | } 16 | 17 | int getValue(int r, int c) { 18 | for(int i=up.size()-1;i>=0;i--) 19 | { 20 | vectora=up[i].first; 21 | if(a[0]<=r&&r<=a[2]&&a[1]<=c&&c<=a[3]) 22 | return up[i].second; 23 | } 24 | return vec[r][c]; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode problems/Subset.java: -------------------------------------------------------------------------------- 1 | // Leetcode : https://leetcode.com/problems/subsets/ 2 | 3 | // given array we need to find total subsets possible using that array 4 | 5 | class Solution { 6 | public List> subsets(int[] nums) { 7 | List> res=new ArrayList<>(); 8 | res.add(new ArrayList<>()); 9 | for (int num:nums){ 10 | int n=res.size(); 11 | for (int i=0;i cur=new ArrayList(res.get(i)); 13 | cur.add(num); 14 | res.add(cur); 15 | } 16 | } 17 | 18 | return res; 19 | } 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /Leetcode problems/Swap Nodes in Pairs.cpp: -------------------------------------------------------------------------------- 1 | //question :-"https://leetcode.com/problems/swap-nodes-in-pairs" 2 | 3 | 4 | class Solution { 5 | public: 6 | ListNode* swapPairs(ListNode* head) { 7 | ListNode* temp = head; 8 | while (temp != NULL && temp->next != NULL){ 9 | swap(temp->val,temp->next->val); 10 | temp = temp->next->next; 11 | 12 | } 13 | return head; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode problems/UniquePaths.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/unique-paths/ 2 | 3 | class Solution { 4 | public: 5 | int uniquePaths(int m, int n) { 6 | int dp[m][n]; 7 | //dp[0][0]=1; 8 | for(int i=0;ival <= l || node->val >= r) return false; 18 | 19 | return chk(node->left, l, node->val) && chk(node->right, node->val, r); 20 | } 21 | public: 22 | bool isValidBST(TreeNode* root) { 23 | return chk(root, LLONG_MIN, LLONG_MAX); 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode problems/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Leetcode problems/a.out -------------------------------------------------------------------------------- /Leetcode problems/arrangeCoins.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class Solution: 4 | def arrangeCoins(self, n: int) -> int: 5 | trow = (-1 + int(math.sqrt(1 + 4*(2*n)))) / 2 6 | return int(trow) 7 | 8 | def main(): 9 | print(Solution().arrangeCoins(5)) 10 | print(Solution().arrangeCoins(8)) 11 | 12 | if __name__ == '__main__': 13 | main() 14 | -------------------------------------------------------------------------------- /Leetcode problems/atoi.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def myAtoi(self, s: str) -> int: 3 | s = s.strip() 4 | res = 0 5 | sign = '+' 6 | length = len(s) 7 | if length == 0: 8 | return 0 9 | if s[0] == '+' or s[0] == '-': 10 | sign = s[0] 11 | s = s[1:] 12 | length -= 1 13 | i = 0 14 | while i < length: 15 | if s[i].isdigit(): 16 | res = res*10 + int(s[i]) 17 | else: 18 | break 19 | i += 1 20 | if sign == '-': 21 | res = -res 22 | if res < -2147483648: 23 | res = -2147483648 24 | elif res > 2147483647: 25 | res = 2147483647 26 | return res 27 | -------------------------------------------------------------------------------- /Leetcode problems/concatenationOfArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] getConcatenation(int[] nums) { 3 | int[] ans = new int[2*nums.length]; 4 | for(int i=0; i 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | string countAndSay(int n) { 7 | if(n==1) 8 | return "1"; 9 | } 10 | }; 11 | 12 | 13 | int main(){ 14 | int n; 15 | cin>>n; 16 | Solution dheeraj; 17 | cout<val) //we simply keep the track of the maximum element along the path 12 | { 13 | mx=root->val; 14 | ++c; //increment the result if number is greater or equal to max element 15 | } 16 | goody(root->left,mx,c); 17 | goody(root->right,mx,c); 18 | } 19 | int goodNodes(TreeNode* root) { 20 | 21 | int c=0,mx=INT_MIN; 22 | goody(root,mx,c); 23 | return c; 24 | } 25 | }; -------------------------------------------------------------------------------- /Leetcode problems/factorial_trailing_zeroes.cpp: -------------------------------------------------------------------------------- 1 | #Function to find Factorial Trailing Zeroes 2 | #Given an integer n, return the number of trailing zeroes in n! 3 | #Problem Link:- https://leetcode.com/problems/factorial-trailing-zeroes/ 4 | int trailingZeroes(int n) { 5 | int ans=0; 6 | while(n) 7 | { 8 | n/=5; 9 | ans+=n; 10 | } 11 | return ans; 12 | } 13 | -------------------------------------------------------------------------------- /Leetcode problems/firstmissingpositive.cpp: -------------------------------------------------------------------------------- 1 | //problem link https://leetcode.com/problems/first-missing-positive/ 2 | // Given an unsorted integer array nums, return the smallest missing positive integer. 3 | 4 | // You must implement an algorithm that runs in O(n) time and uses constant extra space. 5 | 6 | class Solution { 7 | public: 8 | int firstMissingPositive(vector& nums) { 9 | sort(nums.begin(),nums.end()); 10 | int p=0; 11 | int n=nums.size(); 12 | if(n==0){return 1;} 13 | for( int i=0;i-1){p=i;break;}} 15 | int temp=nums[p]; 16 | if(temp<0){return 1;} 17 | if(temp>1){return 1;} 18 | for(int i=p;i& nums) { 5 | int n = nums.size(),maxl=0; 6 | if(n<=0){ 7 | return 0; 8 | } 9 | if(n==1){ 10 | return nums[0]; 11 | } 12 | vector dp(n,0); 13 | dp[0]=nums[0]; 14 | dp[1] = nums[1]; 15 | maxl = max(dp[0],dp[1]); 16 | for(int i=2;imaxl){ 22 | maxl = dp[i]; 23 | } 24 | } 25 | return maxl; 26 | } 27 | -------------------------------------------------------------------------------- /Leetcode problems/jump_game2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | 5 | if (nums.size() == 1) return 0; 6 | 7 | int count = 1; 8 | int limit = nums[0]; 9 | int i = 1; 10 | while(limit < nums.size() - 1){ 11 | int newlimit = limit; 12 | while(i <= limit){ 13 | newlimit = max(i + nums[i], newlimit); 14 | i++; 15 | } 16 | limit = newlimit; 17 | count ++; 18 | } 19 | return count; 20 | } 21 | 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode problems/kdiff-pairs-in-array.cpp: -------------------------------------------------------------------------------- 1 | // problem: https://leetcode.com/problems/k-diff-pairs-in-an-array 2 | 3 | class Solution { 4 | public: 5 | /** 6 | * for every number in the array: 7 | * - if there was a number previously k-diff with it, save the smaller to a set; 8 | * - and save the value-index to a map; 9 | */ 10 | int findPairs(vector& nums, int k) { 11 | if (k < 0) { 12 | return 0; 13 | } 14 | unordered_set starters; 15 | unordered_map indices; 16 | for (int i = 0; i < nums.size(); i++) { 17 | if (indices.count(nums[i] - k)) { 18 | starters.insert(nums[i] - k); 19 | } 20 | if (indices.count(nums[i] + k)) { 21 | starters.insert(nums[i]); 22 | } 23 | 24 | indices[nums[i]] += 1; 25 | } 26 | 27 | return starters.size(); 28 | } 29 | }; -------------------------------------------------------------------------------- /Leetcode problems/longest_common_prefix.cpp: -------------------------------------------------------------------------------- 1 | string longestCommonPrefix(vector& strs) { 2 | if(strs.size()==0) 3 | return ""; 4 | 5 | string prefix = strs[0]; 6 | 7 | for(int i=1;i& nums) { 6 | // Return early if input is empty 7 | if (nums.empty()) return 0; 8 | 9 | int best = INT_MIN; 10 | // Running products 11 | int maxProd = 1; 12 | int minProd = 1; 13 | 14 | for (int n: nums) { 15 | if (n < 0) { 16 | // Swap max and min 17 | swap(maxProd, minProd); 18 | } 19 | // Reset to current value if smaller or larger than it 20 | // (intuitively means that we start considering a new sub-array) 21 | maxProd = max(maxProd*n, n); 22 | minProd = min(minProd*n, n); 23 | // Update the best 24 | best = max(best, maxProd); 25 | } 26 | 27 | return best; 28 | } 29 | }; -------------------------------------------------------------------------------- /Leetcode problems/maximum product subarray.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/maximum-product-subarray/ 2 | 3 | class Solution { 4 | public: 5 | int maxProduct(vector& nums) { 6 | int curr_max=nums[0],curr_min=nums[0], 7 | prev_min=nums[0],prev_max=nums[0],ans=nums[0]; 8 | for(int i=1;idata >= list2->data){ 15 | final = list2; 16 | final->next = merge(list1, list2->next); 17 | } 18 | else { 19 | final = list1; 20 | final->next = merge(list1->next, list2); 21 | } 22 | return(final); 23 | } 24 | -------------------------------------------------------------------------------- /Leetcode problems/minimum_path_sum.py: -------------------------------------------------------------------------------- 1 | # Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, 2 | # which minimizes the sum of all numbers along its path.You can only move either down or right at any point in time. 3 | 4 | 5 | def minPathSum(grid): 6 | m=len(grid) 7 | n=len(grid[0]) 8 | for i in range(0,m): 9 | for j in range(0,n): 10 | if i>0 and j>0: 11 | grid[i][j]+=min(grid[i-1][j],grid[i][j-1]) 12 | elif i>0: 13 | grid[i][j]+=grid[i-1][j] 14 | elif j>0: 15 | grid[i][j]+=grid[i][j-1] 16 | return grid[m-1][n-1] 17 | 18 | if __name__ == "__main__": 19 | grid=[[1,3,1],[1,5,1],[4,2,1]] 20 | print(minPathSum(grid)) 21 | -------------------------------------------------------------------------------- /Leetcode problems/pairSumOptimised.cpp: -------------------------------------------------------------------------------- 1 | // select two numbers such that thier sum is equal to n given that atleast one such pair exists nlogn approach 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | vector towSum(vector s,int num) 9 | { 10 | unordered_set sum; 11 | int i=0,j=s.size(); 12 | vector ind; 13 | for(i=0;i sum={10,5,2,3,-6,9,11}; 28 | vector ind=towSum(sum,11); 29 | cout << ind[0] << ind[1]; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Leetcode problems/pallindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution{ 4 | public: 5 | bool check_pallindrome(int num){ 6 | long long int reverse_num = 0; 7 | long long int temp = num, flag = 0; 8 | if (num <0 ) 9 | return false; 10 | else{ 11 | while(temp !=0){ 12 | reverse_num = reverse_num*10 + temp%10; 13 | temp = temp/10; 14 | } 15 | } 16 | return reverse_num == num; 17 | } 18 | }; 19 | 20 | int main(){ 21 | long long int num = 7463847410; 22 | Solution object; 23 | cout< bool: 9 | 10 | 11 | if len(s1)>len(s2): 12 | return False 13 | a = [0]*26 14 | b = [0]*26 15 | for i in range(len(s1)): 16 | 17 | a[ord(s1[i])-ord('a')]+=1 18 | b[ord(s2[i])-ord('a')]+=1 19 | 20 | 21 | for i in range(len(s2)-len(s1)): 22 | if a==b: 23 | return True 24 | b[ord(s2[i+len(s1)])-97]+=1 25 | b[ord(s2[i])-97]-=1 26 | 27 | print(a) 28 | print(b) 29 | return a==b 30 | -------------------------------------------------------------------------------- /Leetcode problems/prisoncellsndays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]: 3 | N = N % 14 4 | if N == 0: 5 | N = 14 6 | for i in range(N): 7 | cells = [0] + [cells[0] ^ cells[2] ^ 1] + [cells[1] ^ cells[3] ^ 1] + [cells[2] ^ cells[4] ^ 1] + [cells[3] ^ cells[5] ^ 1] + [cells[4] ^ cells[6] ^ 1] + [cells[5] ^ cells[7] ^ 1] + [0] 8 | return cells -------------------------------------------------------------------------------- /Leetcode problems/removeNthNodefromEnd.cpp: -------------------------------------------------------------------------------- 1 | // problem: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 2 | 3 | #include 4 | using namespace std; 5 | class Solution { 6 | public: 7 | ListNode* removeNthFromEnd(ListNode* head, int n) { 8 | ListNode* start = new ListNode(); 9 | start->next = head; 10 | ListNode* last = start; 11 | ListNode* last_prev = start; 12 | 13 | for(int i=1;i<=n;i++){ // Keep iterating till last reaches to n 14 | last = last->next; 15 | } 16 | while(last->next != NULL) // Keep iterating both last and last_prev till last reaches to null(means it's the last element.) 17 | { 18 | last = last->next; 19 | last_prev = last_prev->next; 20 | } 21 | last_prev->next = last_prev->next->next; // remove link to next and assign next to next. 22 | return start->next; 23 | } 24 | }; -------------------------------------------------------------------------------- /Leetcode problems/reverse_integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution{ 4 | public: 5 | int reverse_num(int t){ 6 | int result = 0; 7 | while(t !=0){ 8 | if (INT_MAX/10 < result) return 0; 9 | if (INT_MIN/10 > result ) return 0; 10 | 11 | result = result*10 + (t%10); 12 | t = t/10; 13 | } 14 | return result; 15 | } 16 | }; 17 | 18 | int main(){ 19 | int num = -123; 20 | Solution ob; 21 | cout< List[List[int]]: 9 | if not root: 10 | return [] 11 | queue = [root] 12 | answer = [[root.val]] 13 | while queue: 14 | child = [] 15 | for node in queue: 16 | if node.left: 17 | child.append(node.left) 18 | if node.right: 19 | child.append(node.right) 20 | if not child: #last level of tree. 21 | break 22 | queue = child #assigning this level to queue. 23 | answer.append([node.val for node in queue]) 24 | 25 | return answer[::-1] -------------------------------------------------------------------------------- /Leetcode problems/rotate2darray.cpp: -------------------------------------------------------------------------------- 1 | //Program to rotate 2d array by 90 deg 2 | 3 | class Solution { 4 | public: 5 | bool searchMatrix(vector>& matrix, int target) { 6 | int row = matrix.size(); 7 | int col = matrix[0].size(); 8 | 9 | int rowIndex = 0; 10 | int colIndex = col-1; 11 | 12 | while(rowIndex < row && colIndex >=0) { 13 | int element = matrix[rowIndex][colIndex]; 14 | if(element == target) { 15 | return 1; 16 | } 17 | 18 | if(element < target) { 19 | rowIndex++; 20 | } 21 | else{ 22 | colIndex--; 23 | } 24 | 25 | } 26 | 27 | return 0; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode problems/rotateArray.cpp: -------------------------------------------------------------------------------- 1 | //Problem link: https://leetcode.com/problems/rotate-array/ 2 | //Given an array, rotate the array to the right by k steps, where k is non-negative. 3 | 4 | 5 | class Solution { 6 | public: 7 | void rotate(vector& nums, int k) { 8 | 9 | int len = nums.size(); //finding length of vector nums 10 | vector temp(len); //using an external temp array to store values 11 | for(int i = 0; i>& matrix, int target) { 6 | int row = matrix.size(); 7 | int col = matrix[0].size(); 8 | 9 | int rowIndex = 0; 10 | int colIndex = col-1; 11 | 12 | while(rowIndex < row && colIndex >=0) { 13 | int element = matrix[rowIndex][colIndex]; 14 | if(element == target) { 15 | return 1; 16 | } 17 | 18 | if(element < target) { 19 | rowIndex++; 20 | } 21 | else{ 22 | colIndex--; 23 | } 24 | 25 | } 26 | 27 | return 0; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode problems/stock buy and sell.cpp: -------------------------------------------------------------------------------- 1 | // problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 2 | //Difficulty level: Medium 3 | 4 | class Solution { 5 | public: 6 | int maxProfit(vector& prices) { 7 | int n=prices.size(); 8 | int dp[n]; 9 | dp[0]=0; 10 | for(int i=1;i 2 | using namespace std; 3 | 4 | int main(int argc, char const *argv[]) { 5 | int arr[3] = {3,2,4}; 6 | int t = 6; 7 | int temp, i, j; 8 | 9 | for (i=0; i<6; i++){ 10 | temp = arr[i]; 11 | int val = t-temp; 12 | for(j=i+1; j<6; j++){ 13 | if(arr[j]==val) 14 | cout << i<<'\t' << j; 15 | } 16 | 17 | 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Leetcode problems/valid-anagram.js: -------------------------------------------------------------------------------- 1 | //leetcode problem: Valid Anagram 2 | 3 | var isAnagram = function(s, t) { 4 | 5 | if(s.length !== t.length) return false; 6 | let freq = new Array(26).fill(0); 7 | 8 | let ch = 'a'; 9 | for(let i = 0; i < s.length; i++){ 10 | freq[s[i].charCodeAt(0) - ch.charCodeAt(0)] += 1; 11 | } 12 | 13 | for(let i = 0; i < t.length; i++){ 14 | freq[t[i].charCodeAt(0) - ch.charCodeAt(0)] -= 1; 15 | } 16 | 17 | for(let i = 0; i < 26; i++){ 18 | // console.log(freq[i]); 19 | if(freq[i] != 0) return false; 20 | } 21 | 22 | return true; 23 | 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode problems/word_break.cpp: -------------------------------------------------------------------------------- 1 | #Word Break 2 | #Link to Problem:- https://leetcode.com/problems/word-break/ 3 | bool wordBreak(string s, vector& wordDict) { 4 | int n = s.size(); 5 | vector> dp(n,vector(n,false)); 6 | unordered_set st; 7 | for(int i=0;idata = 1; 14 | two->data = 2; 15 | three->data = 3; 16 | 17 | /* Connect nodes */ 18 | one->next = two; 19 | two->next = three; 20 | three->next = one; 21 | 22 | /* Save address of first node in head */ 23 | head = one; 24 | -------------------------------------------------------------------------------- /Linked_List/Linked_List_Cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | 9 | leetcode problem 142 : Linked List Cycle II 10 | */ 11 | 12 | 13 | class Solution { 14 | public: 15 | ListNode *detectCycle(ListNode *head) { 16 | if(head==NULL){ 17 | return head; 18 | } 19 | 20 | ListNode* temp = head; 21 | map visited; 22 | 23 | while(temp!=NULL){ 24 | if(visited[temp]==true){ 25 | return temp; 26 | } 27 | visited[temp]= true; 28 | temp = temp->next; 29 | } 30 | 31 | return NULL; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Linked_List/Vectors/Printing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void printVec(vectorv){ 5 | cout<<"size:"< v; 12 | int n; 13 | cin>>n; 14 | for(int i =0;i>x; 17 | v.push_back(x); 18 | } 19 | printVec(v); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Linked_List/Vectors/VectorsusingPairs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void printVec(vector>&v){ 5 | cout<<"Size:"<>v; 13 | printVec(v); 14 | int n; 15 | cin>>n; 16 | 17 | for(int i=0;i>x>>y; 20 | v.push_back({x,y}); 21 | } 22 | 23 | printVec(v); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Linked_List/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Linked_List/a.out -------------------------------------------------------------------------------- /Linked_List/detect cycle in Linkedlist.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Detect a cycle in a linked list. Note that the head pointer may be 'NULL' if the list is empty. 3 | A Node is defined as: 4 | struct Node { 5 | int data; 6 | struct Node* next; 7 | } 8 | */ 9 | 10 | bool has_cycle(Node* head) { 11 | // Use Floyd's cycle finding algorithm (aka "tortoise and hare") 12 | Node* slow = head; 13 | Node* fast = head; 14 | while(fast != NULL && fast->next != NULL) { 15 | slow = slow->next; 16 | fast = fast->next->next; 17 | if(slow == fast) { 18 | // Slow pointer caught up to the fast one, so there is a loop 19 | return true; 20 | } 21 | } 22 | return false; // Fast pointer hit the end of the list, so there is no loop 23 | } 24 | -------------------------------------------------------------------------------- /Linked_List/get_length.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node* next; 8 | }; 9 | void add_node(Node** head_ref, int new_data){ 10 | Node* new_node = new Node(); 11 | new_node->data = new_data; 12 | new_node->next = *head_ref; 13 | *head_ref = new_node; 14 | } 15 | void count_len(Node* n){ 16 | int count=0; 17 | while(n!=NULL){ 18 | count++; 19 | n=n->next; 20 | } 21 | cout< 2 | using namespace std; 3 | 4 | class Node { //defining a node 5 | public: 6 | int data; 7 | Node* next; 8 | }; 9 | 10 | 11 | int printdata(Node* n){ 12 | while(n!=NULL){ 13 | cout<data<next; 15 | } 16 | } 17 | 18 | int main(){ 19 | Node* head = NULL; 20 | Node* second = NULL; 21 | Node* third = NULL; 22 | 23 | head = new Node(); //adding nodes 24 | second = new Node(); 25 | third = new Node(); 26 | 27 | head->data =4; //data of nodes 28 | head->next = second; 29 | second->data = 5; 30 | second->next = third; 31 | third->data = 9; 32 | third->next = NULL; 33 | 34 | printdata(head); 35 | } 36 | -------------------------------------------------------------------------------- /Linked_List/nth_node_end.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Linked_List/nth_node_end.exe -------------------------------------------------------------------------------- /ModularExponentiation.cpp: -------------------------------------------------------------------------------- 1 | /*Ajay*/ 2 | #include 3 | using namespace std; 4 | #define mod 1000000007 5 | int modular_expo(int a,int b){ 6 | a=a%mod; 7 | int res=1; 8 | while(b>0){ 9 | if(b&1){ 10 | res=res*a % mod;b--; 11 | } 12 | else{ 13 | a=a*a % mod;b/=2; 14 | } 15 | } 16 | return res; 17 | } 18 | int main(){ 19 | int a,b; 20 | cin>>a>>b; 21 | cout< 2 | #define size 20 3 | using namespace std; 4 | class queue{ 5 | public: 6 | int queue[size]; 7 | int ele = -1; 8 | int len=0; 9 | void push(int value){ 10 | ele++; 11 | queue[ele]=value; 12 | len++; 13 | } 14 | void pop(){ 15 | for(int i=0;i"; 24 | } 25 | cout<<"NULL"; 26 | } 27 | void peek(){ 28 | cout<<"\n first "< 2 | using namespace std; 3 | 4 | // function to print the queue 5 | void Print(queue& Queue) 6 | { 7 | while (!Queue.empty()) { 8 | cout << Queue.front() << " "; 9 | Queue.pop(); 10 | } 11 | } 12 | 13 | // Function to reverse the queue 14 | void reverseQueue(queue& Queue) 15 | { 16 | stack Stack; 17 | while (!Queue.empty()) { 18 | Stack.push(Queue.front()); 19 | Queue.pop(); 20 | } 21 | while (!Stack.empty()) { 22 | Queue.push(Stack.top()); 23 | Stack.pop(); 24 | } 25 | } 26 | 27 | // Driver code 28 | int main() 29 | { 30 | queue Queue; 31 | Queue.push(10); 32 | Queue.push(20); 33 | Queue.push(30); 34 | Queue.push(40); 35 | Queue.push(50); 36 | Queue.push(60); 37 | Queue.push(70); 38 | Queue.push(80); 39 | Queue.push(90); 40 | Queue.push(100); 41 | 42 | reverseQueue(Queue); 43 | Print(Queue); 44 | } 45 | -------------------------------------------------------------------------------- /Recursion/Factorial_using_recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int); 5 | 6 | int main() { 7 | int n, result; 8 | 9 | cout << "Enter a non-negative number: "; 10 | cin >> n; 11 | 12 | result = factorial(n); 13 | cout << "Factorial of " << n << " = " << result; 14 | return 0; 15 | } 16 | 17 | int factorial(int n) { 18 | if (n > 1) { 19 | return n * factorial(n - 1); 20 | } else { 21 | return 1; 22 | } 23 | } -------------------------------------------------------------------------------- /Recursion/Fibonacci.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Fibonacci { 4 | 5 | public static int fibonacci(int number) { 6 | if (number <= 1) 7 | return number; 8 | else 9 | return fibonacci(number - 2) + fibonacci(number - 1); 10 | } 11 | 12 | public static void main(String args[]) { 13 | Scanner in = new Scanner(System.in); 14 | System.out.println("Enter nth digit"); 15 | int number = in.nextInt()+1; 16 | System.out.println(fibonacci(number)); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Recursion/LetterCasePermutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | //siddhartha v 9 | 10 | void solve(int index,int max,string s,string ds,vector &ans) 11 | { 12 | if(index>=max) 13 | { 14 | ans.push_back(ds); 15 | return ; 16 | } 17 | if(s[index]>='0' && s[index]<='9') 18 | { 19 | ds.push_back(s[index]); 20 | solve(index+1,max,s,ds,ans); 21 | } 22 | else{ 23 | 24 | ds.push_back(tolower(s[index])); 25 | solve(index+1,max,s,ds,ans); 26 | ds.pop_back(); 27 | ds.push_back(toupper(s[index])); 28 | solve(index+1,max,s,ds,ans); 29 | } 30 | 31 | 32 | } 33 | 34 | vector letterCasePermutation(string s) { 35 | 36 | string ds; 37 | vector ans; 38 | int max = s.size(); 39 | solve(0,max,s,ds,ans); 40 | return ans; 41 | } 42 | int main() 43 | { 44 | vector v; 45 | 46 | } -------------------------------------------------------------------------------- /Recursion/Target Sum Subsets.cpp: -------------------------------------------------------------------------------- 1 | #include // Accepted 2 | using namespace std; 3 | 4 | void solve(vector& nums, int ind, vector op, int sum, int t) { 5 | 6 | if(sum > t) return; // We should not move further if sum > target sum 7 | 8 | if(ind == nums.size()) { 9 | if(sum == t) { 10 | for(auto i : op) cout< op1 = op; 17 | op1.push_back(nums[ind]); 18 | solve(nums, ind+1, op1, sum+nums[ind],t); 19 | solve(nums, ind+1, op, sum,t); 20 | 21 | 22 | } 23 | 24 | 25 | int main() { 26 | int n,t; 27 | cin>>n; 28 | 29 | vector nums(n); 30 | for(auto& i : nums) cin >> i; 31 | 32 | cin>>t; 33 | 34 | vector op; 35 | 36 | solve(nums,0,op,0,t); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Recursion/Tower_of_Hanoi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // this is the recursive function from tower of hanoi 4 | void towerOfHanoi(int num_of_disk, char rod_before, char rod_after, char rod_auxiliary){ 5 | // if the number of disk is more than 0, then it will call itself 6 | // but if the number of disk is equals or less than 0, then it will stop 7 | if(num_of_disk > 0){ 8 | towerOfHanoi(num_of_disk - 1, rod_before, rod_auxiliary, rod_after); 9 | printf("Moving disk from %c to %c\n", rod_before, rod_after); 10 | towerOfHanoi(num_of_disk - 1, rod_auxiliary, rod_after, rod_before); 11 | } 12 | } 13 | 14 | int main(){ 15 | // you can change the value from the variables below 16 | int num_of_disk = 5; 17 | char rod_before = 'A'; 18 | char rod_after = 'B'; 19 | char rod_auxiliary = 'C'; 20 | 21 | towerOfHanoi(num_of_disk, rod_before, rod_after, rod_auxiliary); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Recursion/factorial_using_recursion.py: -------------------------------------------------------------------------------- 1 | def factorial(n): 2 | if n >1 : 3 | return n* factorial(n-1) 4 | else: 5 | return 1 6 | 7 | n = int(input("Enter a non-negative number: ")) 8 | 9 | result = factorial(n) 10 | print(f"Fctorial of {n} = {result}") 11 | -------------------------------------------------------------------------------- /Recursion/number of dice roll with target sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void diceRoll(string pro, int target) 4 | { 5 | if (target == 0) 6 | { 7 | cout << pro << " "; 8 | return; 9 | } 10 | for (int i = 1; i <= 6 and i <= target; i++) 11 | { 12 | diceRoll(pro + to_string(i), target - i); 13 | } 14 | return; 15 | } 16 | int main() 17 | { 18 | diceRoll("", 4); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Recursion/palindromic_partioning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool ispal(string str){ 5 | int s=0, e=str.length()-1; 6 | while(s<=e && str[s]==str[e]){ 7 | s++; e--; 8 | } 9 | 10 | return s>=e; 11 | } 12 | 13 | void fun(string &s, int cuts, int &ans){ 14 | if(s.length()==0){ 15 | ans=min(ans,cuts); 16 | return; 17 | } 18 | for(int i=1; i<=s.length(); i++){ 19 | string left=s.substr(0,i); 20 | string right=s.substr(i); 21 | if(ispal(left)){ 22 | cuts++; 23 | fun(right, cuts, ans); 24 | cuts--; 25 | } 26 | } 27 | } 28 | 29 | int palindromicPartitioning(string str){ 30 | int ans=INT_MAX; 31 | fun(str, -1, ans); 32 | return ans; 33 | } 34 | 35 | int main(){ 36 | string str; 37 | cin>>str; 38 | cout< 7 | using namespace std; 8 | 9 | int search(int arr[], int n, int x) 10 | { 11 | int i; 12 | for (i = 0; i < n; i++) 13 | if (arr[i] == x) 14 | return i; 15 | return -1; 16 | } 17 | 18 | int main(void) 19 | { 20 | int arr[] = { 2, 3, 4, 10, 40 }; 21 | int x = 10; 22 | int n = sizeof(arr) / sizeof(arr[0]); 23 | int result = search(arr, n, x); 24 | (result == -1)? cout<<"Element is not present in array" 25 | : cout<<"Element is present at index " < 2 | void input(int a[],int n) 3 | { 4 | int i; 5 | for(i=0;ilist[index+1]: 13 | temp = list[index] 14 | list[index] = list[index+1] 15 | list[index+1] = temp 16 | 17 | #the given unsorted list(input list) 18 | list = [19,2,31,45,6,11,121,27] 19 | print(f"The given unsorted list: {list}") 20 | 21 | #the output sorted list. 22 | bubblesort(list) 23 | print(f"The sorted list is: {list}") 24 | -------------------------------------------------------------------------------- /Sort/InsertionSort.py: -------------------------------------------------------------------------------- 1 | import time 2 | def insertionSort(arr): 3 | 4 | # Traverse through 1 to len(arr) 5 | for i in range(1, len(arr)): 6 | 7 | key = arr[i] 8 | 9 | # Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position 10 | j = i-1 11 | while j >=0 and key < arr[j] : 12 | arr[j+1] = arr[j] 13 | j -= 1 14 | arr[j+1] = key 15 | 16 | 17 | # Driver code 18 | arr = [12, 11, 13, 5, 6] 19 | start=time.time() 20 | insertionSort(arr) 21 | print ("Sorted array is:") 22 | for i in range(len(arr)): 23 | print ("%d" %arr[i]) 24 | print("function execution time:",(time.time()-start)) 25 | -------------------------------------------------------------------------------- /Sort/QuickSort.py: -------------------------------------------------------------------------------- 1 | # Quicksort function using recursive. 2 | # Quicksort based on divide-and-conquer algorithm 3 | # Average time complexity is O(NlogN) 4 | def quicksort(arr, start, end): 5 | if start >= end: 6 | return 7 | 8 | # Choose pivot to end of elements. 9 | pivot = arr[end] 10 | 11 | # Find elements smaller than pivot, then Move to left side. 12 | i = start - 1 13 | for j in range(start, end): 14 | if arr[j] < pivot: 15 | i += 1 16 | arr[i], arr[j] = arr[j], arr[i] 17 | arr[i + 1], arr[end] = arr[end], arr[i + 1] # Move pivot to center 18 | 19 | # Divide to two partition. 20 | quicksort(arr, start, i) # Smaller than pivot 21 | quicksort(arr, i + 2, end) # Bigger than pivot 22 | 23 | arr = [52, 17, 19, 3, 22, 8, 2, 41, 23] 24 | print(f"The given unsorted list: {arr}") 25 | 26 | quicksort(arr, 0, len(arr) - 1) 27 | print(f"The sorted list is: {arr}") 28 | -------------------------------------------------------------------------------- /Sort/QuickSort_poojaagrawal134.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Sort/QuickSort_poojaagrawal134.exe -------------------------------------------------------------------------------- /Sort/SelectionSort.py: -------------------------------------------------------------------------------- 1 | import time 2 | def selection_sort(a): 3 | for i in range(len(a)-1): 4 | minindex=i 5 | for j in range(i+1,len(a)): 6 | if a[j] 2 | #include 3 | 4 | #define less(a, b) (al; i--) 10 | cmpexch(v[i-1], v[i]); 11 | 12 | for(int i=l+2; i<=r; i++) { 13 | int j = i; 14 | int tmp = v[j]; 15 | while (less(tmp, v[j-1])){ 16 | v[j] = v[j-1]; 17 | j--; 18 | } 19 | v[j] = tmp; 20 | } 21 | } 22 | 23 | int main() { 24 | int *v,n; 25 | scanf("%d",&n); 26 | v = malloc(n * sizeof(int)); 27 | 28 | for(int i=0; i 1: 3 | 4 | # mid 5 | mid = len(arr)//2 6 | 7 | L = arr[:mid] 8 | R = arr[mid:] 9 | 10 | 11 | mergeSort(L) 12 | mergeSort(R) 13 | 14 | i = j = k = 0 15 | 16 | 17 | while i < len(L) and j < len(R): 18 | if L[i] < R[j]: 19 | arr[k] = L[i] 20 | i += 1 21 | else: 22 | arr[k] = R[j] 23 | j += 1 24 | k += 1 25 | 26 | 27 | while i < len(L): 28 | arr[k] = L[i] 29 | i += 1 30 | k += 1 31 | 32 | while j < len(R): 33 | arr[k] = R[j] 34 | j += 1 35 | k += 1 36 | 37 | 38 | 39 | def printList(arr): 40 | for i in range(len(arr)): 41 | print(arr[i], end=" ") 42 | print() 43 | 44 | 45 | 46 | if __name__ == '__main__': 47 | arr = [12, 11, 13, 5, 6, 7] 48 | print("Given unsorted array is", end="\n") 49 | printList(arr) 50 | mergeSort(arr) 51 | print("Sorted array using merge sort is: ", end="\n") 52 | printList(arr) 53 | 54 | 55 | -------------------------------------------------------------------------------- /Sort/qsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long int ll; 3 | void swap(ll *x, ll *y) 4 | { 5 | ll temp = *x; 6 | *x = *y; 7 | *y = temp; 8 | } 9 | ll qpartition(ll A[], ll low, ll high) 10 | { 11 | ll p = low; //pivot 12 | ll i = low, j = high; 13 | do 14 | { 15 | do 16 | { 17 | 18 | i++; 19 | } while (A[i] <= A[p]); 20 | 21 | do 22 | { 23 | j--; 24 | } while (A[j] > A[p]); 25 | 26 | if (i < j) 27 | swap(&A[i], &A[j]); 28 | 29 | } while (i < j); 30 | 31 | swap(&A[j], &A[p]); 32 | return j; 33 | } 34 | 35 | void quickSort(ll A[], ll low, ll high) 36 | { 37 | ll p; 38 | if (low < high) 39 | { 40 | p = qpartition(A, low, high); 41 | quickSort(A, low, p); 42 | quickSort(A, p + 1, high); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Sort/wavesort.cpp: -------------------------------------------------------------------------------- 1 | /* asked in interview */ 2 | #include 3 | using namespace std; 4 | 5 | void wavesort(int arr[], int n) 6 | { 7 | int i = 1; 8 | while (i <= n+1) 9 | { 10 | if(arr[i] > arr[i-1]) 11 | { 12 | swap(arr[i], arr[i-1]); 13 | } 14 | 15 | if(arr[i] > arr[i+1]) 16 | { 17 | swap(arr[i],arr[i+1]); 18 | } 19 | 20 | i += 2; 21 | } 22 | 23 | } 24 | 25 | int main() 26 | { 27 | int arr[] = {1,3,4,7,5,6,2}; 28 | 29 | int s = sizeof(arr)/sizeof(int); 30 | 31 | wavesort(arr,s); 32 | 33 | for (int i = 0; i < s; i++) 34 | { 35 | cout<= 0; i--) { 14 | System.out.print(stack[i]); 15 | } 16 | } 17 | public static void main(String[] args) { 18 | 19 | Scanner sc = new Scanner(System.in); 20 | System.out.println("Enter a Number : "); 21 | int num = sc.nextInt(); 22 | 23 | while (num > 0) { 24 | int rem = num % 2; 25 | push(rem); 26 | num /= 2; 27 | } 28 | System.out.print("Binary equivalent: "); 29 | pop(); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Stack/Stack.py: -------------------------------------------------------------------------------- 1 | class Stack: 2 | def __init__(self): 3 | self.items = [] 4 | 5 | def isEmpty(self): 6 | return self.items == [] 7 | 8 | def push(self, item): 9 | self.items.append(item) 10 | 11 | def pop(self): 12 | return self.items.pop() 13 | 14 | def peek(self): 15 | return self.items[len(self.items)-1] 16 | 17 | def size(self): 18 | return len(self.items) 19 | 20 | ## 21 | s=Stack() 22 | 23 | print(s.isEmpty()) 24 | s.push(4) 25 | s.push('dog') 26 | print(s.peek()) 27 | s.push(True) 28 | print(s.size()) 29 | print(s.isEmpty()) 30 | s.push(8.4) 31 | print(s.pop()) 32 | print(s.pop()) 33 | print(s.size()) 34 | -------------------------------------------------------------------------------- /Stack/python/balanced_parenthesis.py: -------------------------------------------------------------------------------- 1 | 2 | #Function to check if brackets are balanced or not. 3 | def ispar(self,x): 4 | stack = [] 5 | dic = {'}':'{', ')':'(', ']':'[' } 6 | 7 | for brac in x: 8 | # push into stack if it is opening bracket. 9 | if brac in '[{(': 10 | stack.append(brac) 11 | # else if closing bracket, try to match it with top of stack 12 | # if they don't match, return False 13 | elif len(stack) == 0 or stack[-1] != dic.get(brac, ''): 14 | return False 15 | # if they do match simply pop from stack 16 | else: 17 | stack.pop() 18 | 19 | # if stack isn't empty then it had odd no. of brackets. 20 | if stack: 21 | return False 22 | return True 23 | 24 | if __name__ == '__main__': 25 | s = input().strip() 26 | if ispar(s): 27 | print("balanced") 28 | else: 29 | print("not balanced") -------------------------------------------------------------------------------- /Stack/stl_stack.cpp: -------------------------------------------------------------------------------- 1 | // Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, 2 | // where a new element is added at one end and (top) an element is removed from that end only 3 | 4 | #include 5 | using namespace std; 6 | 7 | void showstack(stack s) 8 | { 9 | while (!s.empty()) 10 | { 11 | cout << '\t' << s.top(); 12 | s.pop(); 13 | } 14 | cout << '\n'; 15 | } 16 | 17 | int main () 18 | { 19 | stack s; 20 | s.push(10); 21 | s.push(30); 22 | s.push(20); 23 | s.push(5); 24 | s.push(1); 25 | 26 | cout << "The stack is : "; 27 | showstack(s); 28 | 29 | cout << "\ns.size() : " << s.size(); 30 | cout << "\ns.top() : " << s.top(); 31 | 32 | 33 | cout << "\ns.pop() : "; 34 | s.pop(); 35 | showstack(s); 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Top Interview Questions/Interview_Questions/best_time_to_buy_sell_stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int n= prices.size(); 5 | int maxPro = 0; 6 | int minprice = INT_MAX; 7 | for(int i=0; i 3 | 4 | int main(void) 5 | { 6 | int i; 7 | for (i=1; i<=100; i++) 8 | { 9 | // number divisible by 3 and 5 will 10 | // always be divisible by 15, print 11 | // 'FizzBuzz' in place of the number 12 | if (i%15 == 0) 13 | printf ("FizzBuzz\t"); 14 | 15 | // number divisible by 3? print 'Fizz' 16 | // in place of the number 17 | else if ((i%3) == 0) 18 | printf("Fizz\t"); 19 | 20 | // number divisible by 5, print 'Buzz' 21 | // in place of the number 22 | else if ((i%5) == 0) 23 | printf("Buzz\t"); 24 | 25 | else // print the number 26 | printf("%d\t", i); 27 | 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Top Interview Questions/Interview_Questions/operator_overloading.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Operator Overloading: 3 | Operator Overloading is a very essential element to perform the operations on 4 | user-defined data types. By operator overloading we can modify the default 5 | meaning to the operators like +, -, *, /, <=, etc. 6 | **/ 7 | 8 | class complex{ 9 | private: 10 | float r, i; 11 | public: 12 | complex(float r, float i){ 13 | this->r=r; 14 | this->i=i; 15 | } 16 | complex(){} 17 | void displaydata(){ 18 | cout<<”real part = “< 2 | using namespace std; 3 | int main() { 4 | int n; 5 | cin>>n; 6 | int arr[n]; 7 | for(int i=0;i>arr[i]; 9 | } 10 | int left_highest[n]; 11 | int right_highest[n]; 12 | int l_high=arr[0]; 13 | int i=0; 14 | int r_high=arr[n-1]; 15 | for(i=0;i=l_high){ 17 | left_highest[i]=arr[i]; 18 | l_high=arr[i]; 19 | } 20 | else 21 | left_highest[i]=l_high; 22 | } 23 | for(i=n-1;i>=0;i--){ 24 | if(arr[i]>=r_high){ 25 | right_highest[i]=arr[i]; 26 | r_high=arr[i]; 27 | } 28 | else 29 | right_highest[i]=r_high; 30 | } 31 | int water_saved=0; 32 | for(i=0;icurr_max: 15 | curr_max = curr 16 | if curr_max >= global_max: 17 | global_max = curr_max 18 | return global_max 19 | 20 | 21 | total_test_cases = int(input()) 22 | for i in range(total_test_cases): 23 | no_of_inputs = int(input()) 24 | li = [int(ele) for ele in input().split()] 25 | print(Cont_Sub_Array(li)) 26 | 27 | -------------------------------------------------------------------------------- /Top Interview Questions/Solutions/Arrays/KthSmallElement.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | 7 | #code 8 | #Question link - https://practice.geeksforgeeks.org/problems/kth-smallest-element/0 9 | def kSmallestElement(li, k): 10 | li.sort() 11 | for i in range(len(li)-k): 12 | li.pop() 13 | if li[-1]: 14 | return li[-1] 15 | else: 16 | return None 17 | 18 | num_of_test_cases = int(input()) 19 | for i in range(num_of_test_cases): 20 | n = int(input()) 21 | li = [int(ele) for ele in input().split()] 22 | k = int(input()) 23 | print(kSmallestElement(li, k)) 24 | 25 | -------------------------------------------------------------------------------- /Top Interview Questions/Solutions/Arrays/MoveAllNegativeNumbersToBeginning.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | 7 | #code 8 | #Question link - https://www.geeksforgeeks.org/move-negative-numbers-beginning-positive-end-constant-extra-space/ 9 | def NegetiveBeforePositive(li): 10 | i = 0 11 | j = len(li)-1 12 | 13 | while i0: 17 | j-=1 18 | if li[i]>0 and li[j]<0: 19 | li[i], li[j] = li[j], li[i] 20 | return li 21 | 22 | li = [-12, 11, -13, -5, 6, -7, 5, -3, -6] 23 | ans = NegetiveBeforePositive(li) 24 | for ele in ans: 25 | print(ele, end=' ') 26 | 27 | -------------------------------------------------------------------------------- /Top Interview Questions/Solutions/Arrays/ReverseTheArray.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | 7 | #code 8 | # Link to question - https://practice.geeksforgeeks.org/problems/reverse-the-string/0 9 | def reverse(li): 10 | for ele in li: 11 | print(ele[::-1]) 12 | 13 | numbers = int(input()) 14 | li = [] 15 | for i in range(numbers): 16 | ele = input() 17 | li.append(ele) 18 | reverse(li) 19 | 20 | -------------------------------------------------------------------------------- /Top Interview Questions/Solutions/Arrays/SortAnArrayOf0s,1sAnd2s.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | 7 | #code 8 | #Question link - https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s/0 9 | def Sort012(li): 10 | li.sort() 11 | return li 12 | 13 | num_of_test_case = int(input()) 14 | for i in range(num_of_test_case): 15 | n = int(input()) 16 | li = [int(ele) for ele in input().split()] 17 | ans = Sort012(li) 18 | for ele in ans: 19 | print(ele, end=" ") 20 | print() 21 | 22 | -------------------------------------------------------------------------------- /Top Interview Questions/Solutions/Arrays/UnionOfTwoArrays.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | 7 | #code 8 | #Question link - https://practice.geeksforgeeks.org/problems/union-of-two-arrays/0 9 | def UnionOf2Arrays(li1, li2): 10 | d1 = {} 11 | d2 = {} 12 | for ele in li1: 13 | d1[ele] = d1.get(ele, 0) + 1 14 | for ele in li2: 15 | d2[ele] = d2.get(ele, 0) + 1 16 | length = len(d1) 17 | for ele in d2: 18 | if ele not in d1: 19 | length += 1 20 | return length 21 | 22 | 23 | num_of_test_cases = int(input()) 24 | for i in range(num_of_test_cases): 25 | a, b = input().split() 26 | li1 = [int(ele) for ele in input().split()] 27 | li2 = [int(ele) for ele in input().split()] 28 | print(UnionOf2Arrays(li1, li2)) 29 | 30 | -------------------------------------------------------------------------------- /Top Interview Questions/Solutions/Matrix/SearchAnElementInAMatriix.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[ ]: 5 | 6 | 7 | #Question link - https://leetcode.com/problems/search-a-2d-matrix/ 8 | class Solution(object): 9 | def searchMatrix(self, matrix, target): 10 | """ 11 | :type matrix: List[List[int]] 12 | :type target: int 13 | :rtype: bool 14 | """ 15 | if not matrix: 16 | return False 17 | 18 | m = len(matrix) 19 | n = len(matrix[0]) 20 | 21 | for i in range(m): 22 | for j in range(n): 23 | if matrix[i][j]>target: 24 | continue 25 | if matrix[i][j] == target: 26 | return True 27 | 28 | return False 29 | 30 | -------------------------------------------------------------------------------- /Trees/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/Trees/a.out -------------------------------------------------------------------------------- /Trees/reverse_level_order.cpp: -------------------------------------------------------------------------------- 1 | vector Solution::solve(TreeNode* A) { 2 | vector ans; 3 | if(!A) return ans; 4 | 5 | queue q; 6 | 7 | q.push(A); 8 | 9 | while(!q.empty()){ 10 | 11 | TreeNode* curr = q.front(); 12 | q.pop(); 13 | ans.push_back(curr->val); 14 | if(curr->right) 15 | { 16 | q.push(curr->right); 17 | } 18 | if(curr->left) 19 | { 20 | q.push(curr->left); 21 | } 22 | } 23 | 24 | reverse(ans.begin(),ans.end()); 25 | 26 | return ans; 27 | } 28 | -------------------------------------------------------------------------------- /array/ArrayTraversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i,j,n; 5 | 6 | printf("Enter no of elements in array\n"); 7 | 8 | scanf("%d",&n); 9 | 10 | //declaring integer array 11 | int int_array[n]; 12 | 13 | printf("Enter the numbers in array\n"); 14 | 15 | for(i=0;i 4 | int gcd_rec(int,int); 5 | void main(){ 6 | 7 | 8 | int firstno,secondno,gcd; 9 | printf("Enter the two no.s to find GCD and LCM:"); 10 | scanf("%d%d",&firstno,&secondno); 11 | 12 | 13 | if(firstno*secondno!=0){ 14 | gcd=gcd_rec(firstno,secondno); 15 | printf("\nThe GCD of %d and %d is %d\n",firstno,secondno,gcd); 16 | printf("\nThe LCM of %d and %d is %d\n",firstno,secondno,(firstno*secondno)/gcd); 17 | } 18 | else 19 | printf("One of the number is 0"); 20 | } 21 | 22 | 23 | /*Function for Euclid's Algorithm*/ 24 | int gcd_rec(int x, int y){ 25 | if (y == 0) 26 | return x; 27 | return gcd_rec(y, x % y); 28 | } 29 | -------------------------------------------------------------------------------- /bitwiseOperators/Find_ith_bit.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a number n and k (1 <= k <= 32), find the value of k-th bit in the binary representation of n. 3 | Bits are numbered from right (Least Significant Bit) to left (Most Significant Bit). 4 | 5 | Examples : 6 | 7 | Input : 8 | n = 13, k = 2 9 | Output : 10 | 0 11 | Explanation: 12 | Binary representation of 13 is 1101. 13 | Second bit from right is 0. 14 | 15 | Input : 16 | n = 14, k = 3 17 | Output : 18 | 1 19 | Explanation: 20 | Binary representation of 14 is 1110. 21 | Third bit from right is 1. 22 | */ 23 | package bitwiseOperators; 24 | 25 | 26 | import java.util.Scanner; 27 | 28 | public class Find_ith_bit { 29 | public static void main(String[] args) { 30 | Scanner sc = new Scanner(System.in); 31 | int num = sc.nextInt(); 32 | int k = sc.nextInt(); 33 | System.out.println((num >> (k-1)) % 2); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /bitwiseOperators/OddEven.java: -------------------------------------------------------------------------------- 1 | package bitwiseOperators; 2 | 3 | import java.util.Scanner; 4 | 5 | public class OddEven { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int num = sc.nextInt(); 9 | 10 | if((num & 1) == 1){ 11 | System.out.println("Odd"); 12 | }else{ 13 | System.out.println("Even"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /bitwiseOperators/SwapNumbers.java: -------------------------------------------------------------------------------- 1 | /* 2 | Swaping the Two variable without using bitwise operator. 3 | */ 4 | 5 | package bitwiseOperators; 6 | 7 | import java.util.Scanner; 8 | 9 | public class SwapNumbers { 10 | public static void main(String[] args) { 11 | Scanner sc = new Scanner(System.in); 12 | int a = sc.nextInt(); 13 | int b = sc.nextInt(); 14 | System.out.println("Value of 'a' After Swap: " + a + " and Value of 'b' after Swap: " + b); 15 | a = a ^ b; 16 | b = a ^ b; 17 | a = a ^ b; 18 | 19 | System.out.println("Value of 'a' After Swap: " + a + " and Value of 'b' after Swap: " + b); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /bitwiseOperators/binarys1sCompliment.java: -------------------------------------------------------------------------------- 1 | package bitwiseOperators; 2 | 3 | import java.util.Scanner; 4 | 5 | public class binarys1sCompliment { 6 | public static void main(String[] args) { 7 | 8 | Scanner sc = new Scanner(System.in); 9 | int num = sc.nextInt(); 10 | int x = 0; 11 | int sum = 0; 12 | while(num > 0){ 13 | int rem = num % 2; 14 | if(rem == 0){ 15 | sum += Math.pow(2,x); 16 | x++; 17 | } 18 | else{ 19 | x++; 20 | } 21 | num = num / 2; 22 | } 23 | System.out.println(sum); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /bitwiseOperators/swap_ith_and_jth_bit_position.py: -------------------------------------------------------------------------------- 1 | def swap_ij_bits(x, i, j): 2 | bitmask = (1 << i) | (1 << j) 3 | x ^= bitmask 4 | return x 5 | 6 | 7 | print(swap_ij_bits(5, 1, 2)) 8 | -------------------------------------------------------------------------------- /contests/codechef/DSA-Learning_C1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/contests/codechef/DSA-Learning_C1/a.out -------------------------------------------------------------------------------- /contests/codechef/DSA-Learning_C1/life_uiver_evry.py: -------------------------------------------------------------------------------- 1 | flag =0 2 | n = int(input()) 3 | while n: 4 | if n==42: 5 | flag =1 6 | if flag==0: 7 | print(n) 8 | n = int(input()) 9 | -------------------------------------------------------------------------------- /contests/codechef/DSA-Learning_C1/reverse_integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef long long ll; 3 | using namespace std; 4 | int main() 5 | { 6 | ll t,num,result=0; 7 | cin>>t; 8 | while(t--){ 9 | cin>>num; 10 | int result = 0; 11 | while(num !=0){ 12 | if (INT_MAX/10 < result) return 0; 13 | if (INT_MIN/10 > result ) return 0; 14 | 15 | result = result*10 + (num%10); 16 | num = num/10; 17 | } 18 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n, k, x, y,i=1, flag=0; 9 | cin>>n>>k>>x>>y; 10 | int temp = n; 11 | // if (n<1) cout<<"NO"< n-1) || (y>n-1)) cout<<"NO"< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n,d; 9 | cin>>n>>d; 10 | int new_n=n,rem,ans=0,c=0; 11 | // if(n/d == 1){ 12 | // cout<<1<0){ 16 | rem=new_n%10; 17 | new_n=new_n/10; 18 | c++; 19 | if(rem==d){ 20 | new_n=new_n*pow(10,c)+(rem+1)*pow(10,c-1); 21 | ans=new_n-n; 22 | c=0; 23 | } 24 | } 25 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | int a,b; 8 | for(int i=0;i>a>>b; 10 | if(a>0 && b>0){ 11 | cout<<"Solution\n"; 12 | } 13 | else if(a==0 && b>0){ 14 | cout<<"Liquid\n"; 15 | } 16 | else{ 17 | cout<<"Solid\n"; 18 | } 19 | 20 | } 21 | // while(t>0){ 22 | // int a,b; 23 | // cin>>a>>b; 24 | // if(a>0 && b>0){ 25 | // cout<<"Solution\n"; 26 | // } 27 | // else if(a==0 && b>0){ 28 | // cout<<"Liquid\n"; 29 | // } 30 | // else{ 31 | // cout<<"Solid\n"; 32 | // } 33 | // } 34 | } -------------------------------------------------------------------------------- /contests/codechef/sep19long/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/contests/codechef/sep19long/a.out -------------------------------------------------------------------------------- /contests/codechef/sep19long/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define long long long 7 | const long M = 1000000007; // modulo 8 | map F; 9 | 10 | long f(long n) { 11 | if (F.count(n)) return F[n]; 12 | long k=n/2; 13 | if (n%2==0) { // n=2*k 14 | return F[n] = (f(k)*f(k) + f(k-1)*f(k-1)) % M; 15 | } else { // n=2*k+1 16 | return F[n] = (f(k)*f(k+1) + f(k-1)*f(k)) % M; 17 | } 18 | } 19 | 20 | main(){ 21 | int t; 22 | cin>>t; 23 | while(t--){ 24 | long n,ans; 25 | cin>>n; 26 | ans= (long)(log(n)/log(2)); 27 | //cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | long int n, temp=1; 9 | cin>>n; 10 | long int dh[n]; 11 | 12 | for(int i=0; i>dh[i]; 14 | } 15 | 16 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | long int n, sum=0; 9 | cin>>n; 10 | long int arr[n]; 11 | // vector v; 12 | for(int i=0; i>arr[i]; 15 | // v.push_back(input); 16 | } 17 | 18 | // for(auto i: v){ 19 | // sum = sum+ i; 20 | // } 21 | 22 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while(t--) { 8 | long long n; 9 | cin >> n; 10 | long long sum = (n)*(n+1)/2; 11 | if(sum%2!=0) { 12 | cout << 0< 2 | using namespace std; 3 | 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | while(t--){ 8 | int n; 9 | cin>>n; 10 | int arr[n]; 11 | 12 | for(int i=0; i>arr[i]; 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /contests/codechef/subtask.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int t; 8 | cin>>t; 9 | for (int i = 0; i < t; i++) 10 | { 11 | /* code */ 12 | 13 | int n,m,k,count=0,total=0; 14 | 15 | cin>>n>>m>>k; 16 | int arr[n]; 17 | for (int j = 0; j < n; j++) 18 | { 19 | /* code */ 20 | cin>>arr[j]; 21 | } 22 | for (int k = 0; k < n; k++) 23 | { 24 | /* code */ 25 | count+=arr[k]; 26 | 27 | } 28 | if(n==count){ 29 | cout<<"100"< 2 | // using namespace std; 3 | // int main(){ 4 | // int t; 5 | // cin>>t; 6 | // while(t--){ 7 | // long long int l,c,p; 8 | // cin>>l>>c; 9 | // p = l-1+c; 10 | // cout<< p< 14 | using namespace std; 15 | 16 | int main() { 17 | // your code goes here 18 | long long int t; 19 | cin>>t; 20 | while(t--){ 21 | long long int a,b,max,min; 22 | cin>>a>>b; 23 | if(a>b){ 24 | max=a; 25 | min=b;} 26 | else{ 27 | max=b; 28 | min=a;} 29 | cout< 2 | using namespace std; 3 | #define P(x) cout<>x; 5 | 6 | class Player{ 7 | public: 8 | int x, y; 9 | int speed; 10 | 11 | // Functions inside the classes are called methods 12 | void move(int xa, int ya){ 13 | x = xa+ speed; 14 | y = ya + speed; 15 | } 16 | 17 | 18 | }; 19 | 20 | 21 | void move(Player& player, int xa, int ya){ // we can also use it as class methods 22 | player.x = xa+ player.speed; 23 | player.y = ya + player.speed; 24 | } 25 | 26 | int main(){ 27 | Player player; 28 | player.x = 5; 29 | P(player.x); 30 | 31 | move(player, 1, -1); 32 | 33 | player.move(1, -1); // Use this if using class methods 34 | } 35 | -------------------------------------------------------------------------------- /trash_codes/Cpp/compiler/bracket.h: -------------------------------------------------------------------------------- 1 | } 2 | -------------------------------------------------------------------------------- /trash_codes/Cpp/compiler/multiply.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int multiply(int a, int b){ 5 | int result = a *b; 6 | return result; 7 | #include "bracket.h" 8 | 9 | // #if 1 10 | int main(){ 11 | int n1, n2; 12 | cin>>n1>>n2; 13 | cout< 2 | using namespace std; 3 | 4 | // enum val{ 5 | // A, B, C 6 | // }; 7 | 8 | // int a =0; 9 | // int b=1; 10 | // int c=2; 11 | 12 | // int main(){ 13 | // val value = B; 14 | // 15 | // cout< 2 | using namespace std; 3 | 4 | class Entitity{ 5 | public: 6 | float X, Y, z; 7 | void move(float xa, float ya){ 8 | X = xa+5; 9 | Y += ya; 10 | } 11 | }; 12 | 13 | class Player : public Entitity{ 14 | public: 15 | const char* name; 16 | // float X, Y; 17 | // 18 | // void move(float xa, float ya){ 19 | // X += xa; 20 | // Y += ya; 21 | // } 22 | 23 | void print_name(){ 24 | cout<< name< 2 | void log(const char* message){ 3 | cout< 2 | using namespace std; 3 | 4 | 5 | void log(const char* message); 6 | 7 | static int mul(int a, int b){ 8 | log("dheeraj"); 9 | return a * b; 10 | } 11 | 12 | int main(){ 13 | cout< 2 | using namespace std; 3 | #define P(x) cout<>x; 5 | 6 | int main(int argc, char const *argv[]) { 7 | /* code */ 8 | int x; 9 | int* ptr = &x; 10 | // cin>>x; 11 | take_input 12 | P(*ptr) 13 | return 0; 14 | 15 | char* buff = new char[8]; 16 | memset(buff, 0, 8); 17 | 18 | char** ptr = &buff; 19 | // P(**ptr) 20 | // P(*buff) 21 | } 22 | -------------------------------------------------------------------------------- /trash_codes/Cpp/references.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define P(x) cout<>x; 5 | 6 | 7 | int inc(int& val){ 8 | val ++; 9 | // return val; 10 | } 11 | 12 | int res(int* val){ 13 | // val ++; 14 | (*val)++; 15 | // return val; 16 | } 17 | 18 | 19 | int main(int argc, char const *argv[]) { 20 | int a = 10; 21 | // int* ptr = &a; 22 | inc(a); 23 | P(a) 24 | 25 | res(&a); 26 | P(a) 27 | 28 | 29 | int* ptr = &a; 30 | int& ref = a; 31 | P(a) 32 | P(ptr) 33 | P(&ref) 34 | 35 | 36 | ref = 13; 37 | P(ref) 38 | P(a) 39 | } 40 | -------------------------------------------------------------------------------- /trash_codes/Cpp/static/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dheeraj-2000/dsalgo/2b71317fb372ceefdaaa4310217872abc48c5007/trash_codes/Cpp/static/a.out -------------------------------------------------------------------------------- /trash_codes/Cpp/static/local_static.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | // static int i=0; 4 | 5 | void func(){ 6 | static int i=0; 7 | i++; 8 | cout< 2 | using namespace std; 3 | 4 | // extern int s_variable; 5 | 6 | static int s; 7 | void dunct(){ 8 | 9 | } 10 | 11 | int main(){ 12 | // cout<>s; 14 | cout< 2 | using namespace std; 3 | 4 | class s{ 5 | public: 6 | static int x, y; 7 | 8 | static void print(){ 9 | cout< 2 | using namespace std; 3 | 4 | class SocialNetworking{ 5 | private: 6 | 7 | public: 8 | virtual void secret() {}; 9 | }; 10 | 11 | class Facebook : public SocialNetworking{ 12 | private: 13 | string fbPassword; 14 | void secret(){ 15 | cout<<"The Facebook Password: "<secret(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /trash_codes/Math/binary_represntation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void binary(int n){ 5 | for(int i=13; i>-1; i--) 6 | cout<<((n>>i)&1); 7 | cout << endl; 8 | } 9 | 10 | int main(){ 11 | int t; 12 | cin>>t; 13 | while(t--){ 14 | int n; 15 | cin>>n; 16 | binary(n); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /trash_codes/Math/celcius_to_farenheit.py: -------------------------------------------------------------------------------- 1 | print("press(1) for celsius to fahrenheit") 2 | print("press(2) for fahrenheit to celsius") 3 | print("enter the choice:") 4 | 5 | choice=int(input()) 6 | 7 | if(choice==1): 8 | print("Enter the celcius value: ") 9 | celsius=input() 10 | 11 | fahrenheit=(celsius*1.8+32) 12 | print("Celcius to forenheit is: ",fahrenheit) 13 | 14 | if(choice==2): 15 | print("Enter the Forenheit value: ") 16 | fahrenheit=input() 17 | celsius=((fahrenheit-32)/1.8) 18 | print("Forenheit to Celcius: ",celsius) 19 | -------------------------------------------------------------------------------- /trash_codes/Math/decimal_to_binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | // Solution obj; 9 | // cout << obj.decimal_to_binary(n); 10 | vector v; 11 | while(n>0){ 12 | int temp = n%2; 13 | v.push_back(temp); 14 | n = n/2; 15 | } 16 | for(auto i: v) 17 | cout< 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | int find_gcd(int n, int m){ 7 | if (m == 0) 8 | return n; 9 | return find_gcd(m, n%m); 10 | 11 | } 12 | }; 13 | 14 | 15 | 16 | int main(){ 17 | int a, b; 18 | cin>>a>>b; 19 | Solution obj; 20 | cout< 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | int find_gcd(int n, int m){ 7 | if (m == 0) 8 | return n; 9 | return find_gcd(m, n%m); 10 | 11 | } 12 | }; 13 | 14 | 15 | 16 | 17 | int main(){ 18 | int a, b; 19 | cin>>a>>b; 20 | Solution obj; 21 | cout<<(a*b)/obj.find_gcd(a, b); 22 | } 23 | -------------------------------------------------------------------------------- /trash_codes/Math/nth_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | // // O(2^n) Time | O(n) Space 7 | // int nth_fibo(int num){ 8 | // if(num == 1) 9 | // return 0; 10 | // if(num == 2) 11 | // return 1; 12 | // 13 | // else{ 14 | // int fib = nth_fibo(num-1) + nth_fibo(num-2) ; 15 | // return fib; 16 | // } 17 | // } 18 | 19 | // O(n) Time | O(1) Space 20 | int nth_fibo(int num){ 21 | vector v{0, 1}; 22 | int count=3; 23 | while(count<= num){ 24 | int fib = v[0] + v[1]; 25 | v[0] = v[1]; 26 | v[1] = fib; 27 | count++; 28 | } 29 | return num >1 ? v[1] : v[0]; 30 | } 31 | 32 | int main(){ 33 | int n; 34 | cin>>n; 35 | cout< 2 | using namespace std; 3 | 4 | 5 | int main(){ 6 | int m, new_num=0; 7 | cin>>m; 8 | int n = m; 9 | // vector v; 10 | while(n!=0){ 11 | 12 | // int temp = n %10; 13 | new_num = 10*new_num + n%10; 14 | n = n/10; 15 | 16 | } 17 | // cout << n< 2 | using namespace std; 3 | 4 | class Solution{ 5 | public: 6 | bool find_prime(int n){ 7 | if (n==1) 8 | return 0; 9 | if (n==2) 10 | return 1; 11 | int temp =2; 12 | while(temp <= n/2){ 13 | if (n% temp == 0){ 14 | return 0; 15 | break; 16 | } 17 | temp++; 18 | } 19 | return 1; 20 | } 21 | }; 22 | 23 | 24 | 25 | 26 | int main(){ 27 | int a; 28 | cin>>a; 29 | Solution obj; 30 | cout< 2 | #include 3 | 4 | /** create a thread 5 | * 6 | */ 7 | 8 | static void *ThreadFunc(){ //whatever you want the thread to do 9 | 10 | sleep(1); 11 | printf("Printing Thread"); 12 | return NULL; 13 | } 14 | int main(){ 15 | 16 | pthread_t thread_id; 17 | printf("Before thread"); 18 | pthread_create(&thread_id,NULL,ThreadFunc,NULL); 19 | pthread_join(thread_id,NULL); 20 | printf("After Thread"); 21 | exit(0); 22 | 23 | } 24 | 25 | // execute with gcc -pthread main.c && ./a.out 26 | -------------------------------------------------------------------------------- /trash_codes/host.txt: -------------------------------------------------------------------------------- 1 | hostnkjdljsn -- [cdsf] lkjlskdjk 6789 2 | hostnkjdljsn -- [cdsf] lkjlskdjk 3000 3 | -------------------------------------------------------------------------------- /trash_codes/kth_element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int search(); 6 | int main() 7 | { 8 | int t; 9 | cin >> t; 10 | while (t--) 11 | { 12 | int n, k; 13 | cin >> n; 14 | vector arr; 15 | for (int i = 0; i < n; i++) 16 | arr.push_back(search()); 17 | cin >> k; 18 | sort(arr.begin(), arr.end()); 19 | cout << arr[k - 1] << endl; 20 | } 21 | return 0; 22 | } 23 | 24 | int search() 25 | { 26 | int ret; 27 | cin >> ret; 28 | return ret; 29 | } 30 | -------------------------------------------------------------------------------- /trash_codes/remove_element.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # def removeElement(self, nums: List[int], val: int) -> int: 3 | def removeElement(self, nums, val): 4 | nums[:] = [x for x in nums if x != val] 5 | return len(nums) 6 | -------------------------------------------------------------------------------- /trash_codes/tes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void my_recursive_function() 5 | { 6 | my_recursive_function(); 7 | } 8 | int main() 9 | { 10 | my_recursive_function(); 11 | return 0; 12 | } 13 | --------------------------------------------------------------------------------